By learning to implement spring's custom tags, you can know that the <mvc:interceptors /> tag is registered in the Spring container by inheriting the MvcNamespaceHandler of NamespaceHandlerSupport. The corresponding parser is the InterceptorsBeanDefinitionParser class. The trigger process is detailed here
A RootBeanDefinition class of MappedInterceptor type will be defined in the parse method of InterceptorsBeanDefinitionParser. This RootBeanDefinition is very familiar. .
After setting the parameters of the constructor, the bean is saved in the two properties of beanDefinitionNames and beanDefinitionMap in DefaultListableBeanFactory through the registerWithGeneratedName method for later calling.
In the container ApplicationContext, onfresh will call the finishBeanFactoryInitialization method to register the bean
The previous beanDefinitionNames and beanDefinitionMap are about to start working
At this point, the initialization of the interceptor in the container is over. The following is the trigger logic in the specific request.
DispatcherServlet will first call the onRefresh method, and internally call the initStrategies method for initialization
initHandlerMappings will initialize all handlerMappings and get all beans of type HandlerMapping in the container
Looking at the subclass structure of HandlerMapping, you can find that all subclasses will inherit or inherit AbstractHandlerMapping in disguise
The detectMappedInterceptors method in the initApplicationContext method in AbstractHandlerMapping will get all the interceptors. And put it into adaptedInterceptors for later use
When the request comes, the doService method of DispatcherServlet will be called, and then the doDispatch method will be called. Internally, doDispatch converts the previous HandlerMapping into a HandlerExecutionChain to specifically handle interceptors.
So happy to see this adaptedInterceptors. .
Then call applyPreHandle to intercept processing. Get the interceptor beans through this.getInterceptors(), traverse these beans and call their preHandle method
End...