spring源码学习系列3.1-handlerMapping初始化

SimpleUrlHandlerMapping的继承体系
org.springframework.web.servlet.handler
Class SimpleUrlHandlerMapping

java.lang.Object
  org.springframework.context.support.ApplicationObjectSupport
    org.springframework.web.context.support.WebApplicationObjectSupport
      org.springframework.web.servlet.handler.AbstractHandlerMapping
        org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
          org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
All Implemented Interfaces:
Aware, ApplicationContextAware, Ordered, ServletContextAware, MatchableHandlerMapping, HandlerMapping
Direct Known Subclasses:
WebSocketHandlerMapping



SimpleUrlHandlerMapping是 springioc启动时,初始化单例类时实例化?还是在initHandlerMappings(context);
Map<String, HandlerMapping> matchingBeans =
					BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
初始化?


<listener/>与<servlet/>是并行执行吗?还是listener执行完加载servlet



ApplicationObjectSupport#setApplicationContext
public final void setApplicationContext(ApplicationContext context) throws BeansException {
    ...
    initApplicationContext(context);
    ...
}

由于ApplicationObjectSupport实现了ApplicationContextAware,初始化SimpleUrlHandlerMapping时,会调用setApplicationContext

ApplicationObjectSupport#initApplicationContext
protected void initApplicationContext(ApplicationContext context) throws BeansException {
		initApplicationContext();
	}



SimpleUrlHandlerMapping#initApplicationContext
@Override
	public void initApplicationContext() throws BeansException {
//1 初始化拦截器
		super.initApplicationContext();
//2 初始化请求地址与处理器的映射,初始化AbstractUrlHandlerMapping.handlerMap
		registerHandlers(this.urlMap);
	}


AbstractHandlerMapping#initApplicationContext
@Override
	protected void initApplicationContext() throws BeansException {
		extendInterceptors(this.interceptors);
//1.1 处理mapperInterceptors
		detectMappedInterceptors(this.mappedInterceptors);
//1.2 处理mapperInterceptors和adaptedInterceptors
		initInterceptors();
	}


AbstractHandlerMapping#detectMappedInterceptors
//1.1 处理mapperInterceptors
protected void detectMappedInterceptors(List<MappedInterceptor> mappedInterceptors) {
		mappedInterceptors.addAll(
				BeanFactoryUtils.beansOfTypeIncludingAncestors(
						getApplicationContext(), MappedInterceptor.class, true, false).values());
	}



AbstractHandlerMapping#detectMappedInterceptors
//1.2 处理mapperInterceptors和adaptedInterceptors
protected void initInterceptors() {
		if (!this.interceptors.isEmpty()) {
			for (int i = 0; i < this.interceptors.size(); i++) {
				Object interceptor = this.interceptors.get(i);
				if (interceptor == null) {
					throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
				}
				if (interceptor instanceof MappedInterceptor) {
					this.mappedInterceptors.add((MappedInterceptor) interceptor);
				}
				else {
					this.adaptedInterceptors.add(adaptInterceptor(interceptor));
				}
			}
		}
	}


参考:
拦截器:
http://www.cnblogs.com/fangjian0423/p/springMVC-interceptor.html
https://my.oschina.net/alextong/blog/97310

猜你喜欢

转载自newjava-sina-cn.iteye.com/blog/2375343