Spring Boot interceptor does not take effect

Preliminary phenomenon

新建的Spring Boot拦截器不生效

Preliminary investigation

- 排查拦截器是否注册
- 拦截器的excludePathPatterns等属性是否设置正确
这些都没问题,进行下一步排查

Further investigation

debug DispatchServelt, check if the interceptor exists in the handler.
It is found that there are two RequestMappingHandlerMapping instances, one of which contains the newly created interceptor, and the other does not have any interceptor.

Depth why there are two RequestMappingHandlerMapping instances

  • When is RequestMappingHandlerMapping initialized
    • Is initialized in WebMvcAutoConfiguration, and will only be initialized once
  • When does RequestMappingHandlerMapping add an interceptor
    • When RequestMappingHandlerMapping is initialized, it will obtain all the interceptors in the current environment and write it to its own variable

It can be seen that there is a RequestMappingHandlerMapping generated in an abnormal way. Using spring boot, an application will generate a RequestMappingHandlerMapping instance through WebMvcAutoConfiguration when the application starts. Where is the other generated?

Finally found that there is an xml configuration file with the following statement:

<mvc:annotation-driven/>

After using mvc: annotation-driven, the default will help us register the default processing request processor.
The problem has been found here

to sum up

Under the spring boot framework, the application will automatically generate a RequestMappingHandlerMapping instance, this instance will get all the interceptors in the current environment and write it to its own variables, and mvc: annotation-driven will register the default processing request handler, Resulting in multiple RequestMappingHandlerMapping, causing conflicts

Published 190 original articles · 19 praises · 200,000+ views

Guess you like

Origin blog.csdn.net/zengchenacmer/article/details/104190494