SpingMVC第一个拦截器未执行BUG分析

问题描述:

SpringMvc项目中使用<mvc:interceptors>配置了一系列的拦截器,Web运行时发现第一个拦截器未执行。

 

问题分析:

1、第一个拦截器未注册上?

 

 

InterceptorsBeanDefinitionParser 中打断点,观察有生成对应的拦截器。

2、拦截器未命中?

AbstractHandlerMapping.getHandlerExecutionChain(Object handler, HttpServletRequest request)

 
发现规则命中,拦截器数量与MVC中配置数量一致,但是第一个拦截器确是ConversionServiceExposingInterceptor,应用中未配置该拦截器。
mappedInterceptors是在initApplicationContext()中初始化,跟踪代码进入如下片段:

 中间有一次递归调用,发现result来自两个BeanFactory,mvc中配置了四个拦截器,在此处跟踪发现在map中被覆盖,KEY值相同,为org.springframework.web.servlet.handler.MappedInterceptor#0,对应Class为ConversionServiceExposingInterceptor.class;
ConversionServiceExposingInterceptor在何处生成,为什么会有相同的BeanName,代码片段如下:

 

 String org.springframework.beans.factory.support.BeanDefinitionReaderUtils.generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry, boolean isInnerBean) throws BeanDefinitionStoreException

扫描二维码关注公众号,回复: 354770 查看本文章

 


 

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覆盖。

 结论:涉及MVC相关的配置都要配置在DispatcherServlet中,不要一部分放到ContextLoaderListener,可能会因为名称冲突被覆盖。

猜你喜欢

转载自uuhorse.iteye.com/blog/2311494