来张图
前置
最近在开发一个web项目,集成hibernate,且使用了OpenSessionInViewFilter,发现spring的事务无效了。进行了如下配置:
spring上下文的applicationContext.xml
<context:component-scan base-package="com.github.mypost">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="execution(* com.github.mypost.*.service..*+.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- annotation trasaction -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
springmvc的上下文配置webApplicationContext.xml
<context:component-scan base-package="com.github.mypost">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--默认的注解映射的支持 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 拦截器 -->
<mvc:interceptors>
<bean class="com.github.mypost.commons.web.interceptor.BaseInterceptor"></bean>
</mvc:interceptors>
<!--视图解释器,根据不同的请求展示不同的View -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<!--视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
</bean>
<mvc:view-controller path="/" view-name="index"/>
一直以为是使用了OpenSessionInViewFilter这个类库的原因导致的失败,最后才发现是由于context:component-scan的use-default-filters导致的问题。
分析
- <context:component-scan>会交给org.springframework.context.config.ContextNamespaceHandler处理;
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
- <context:component-scan>的use-default-filters默认true,会交给ClassPathScanningCandidateComponentProvider进行扫描,看代码:
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));
logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
可以看到默认ClassPathBeanDefinitionScanner会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描。如果细心,到此就找到问题根源了。
- 对exclude-filter,include-filter进行过滤
首先通过exclude-filter 进行黑名单过滤;
然后通过include-filter 进行白名单过滤;
否则默认排除。
结论
<context:component-scan base-package="com.github.mypost" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
那么这段代码不仅仅对@Controller的注解Bean进行了扫描,而且对@Compoent注解也扫描了,子注解@Service,@Repository也进行了扫描,所以原来mvc的上下文的Bean被后面的bean替换了。所以如下解决方法:
- 如果不需要默认的,则use-default-filters=“false”禁用掉。
- 直接写明具体Controller包名称。