问题描述:
SpringMvc项目中使用<mvc:interceptors>配置了一系列的拦截器,Web运行时发现第一个拦截器未执行。
问题分析:
1、第一个拦截器未注册上?
InterceptorsBeanDefinitionParser 中打断点,观察有生成对应的拦截器。
2、拦截器未命中?
![](https://i-blog.csdnimg.cn/blog_migrate/8cac1c5db02368257f5eb8fcfddd438e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/5916b450b7b0704c617be938190298b0.png)
中间有一次递归调用,发现result来自两个BeanFactory,mvc中配置了四个拦截器,在此处跟踪发现在map中被覆盖,KEY值相同,为org.springframework.web.servlet.handler.MappedInterceptor#0,对应Class为ConversionServiceExposingInterceptor.class;
![](https://i-blog.csdnimg.cn/blog_migrate/efb5f49d982a90ae0c1dac5f5389faa9.png)
String org.springframework.beans.factory.support.BeanDefinitionReaderUtils.generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry, boolean isInnerBean) throws BeanDefinitionStoreException
Generate a bean name for the given bean definition, unique within the given bean factory.
此代码只在一个bean工厂中保证名称唯一。
SpringMVC web.xml同时配置了ContextLoaderListener 和 DispatcherServlet,误将<mvc:interceptors>的配置放到了ContextLoaderListener 下,而DispatcherServlet解析<mvc:annotation-driven />时,默认注册ConversionServiceExposingInterceptor.class,生成了ID为org.springframework.web.servlet.handler.MappedInterceptor#0的拦截器,导致两个容器中的beanName重复,即处理请求时通过Map承载Interceptors的过程中导致第一个拦截器ConversionServiceExposingInterceptor覆盖。