Spring source code learning series 3.1-handlerMapping initialization

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 is instantiated when springioc starts, when the singleton class is initialized? Or in initHandlerMappings(context);
Map<String, HandlerMapping> matchingBeans =
					BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
When initialized ? Are


<listener/> and <servlet/> executed in parallel? Or is the listener loaded after the servlet is executed



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

Since ApplicationObjectSupport implements ApplicationContextAware, when SimpleUrlHandlerMapping is initialized, setApplicationContext

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



SimpleUrlHandlerMapping#initApplicationContext
@Override
	public void initApplicationContext() throws BeansException {
//1 Initialize the interceptor
		super.initApplicationContext();
//2 Initialize the mapping between the request address and the handler, initialize AbstractUrlHandlerMapping.handlerMap
		registerHandlers(this.urlMap);
	}


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


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



AbstractHandlerMapping#detectMappedInterceptors
//1.2 Handling mapperInterceptors and 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));
				}
			}
		}
	}


Reference:
Interceptor:
http://www.cnblogs.com/fangjian0423/p/springMVC-interceptor.html
https://my.oschina.net/alextong/blog/97310

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326446764&siteId=291194637