如果用注解,扫描所有包,会导致事务不起作用,spring-servlet.xml,只扫描Controller类型的bean:
<!-- 扫描所有的controller -->
<context:component-scan base-package="com.test.sso.web">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
如果base-package中的包定位到web,则不需要额外加其他配置,如果base-package="com.test",则不仅仅扫描@Controller注解的Bean,而且还扫描了@Component的子注解@Service、@Reposity。因为use-default- filters默认为true。所以如果不需要默认的,则use-default-filters=“false”禁用掉。
因此也可将上面的配置修改为:
<context:component-scan base-package="com.test" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
applicationContext-dbaop.xml,去除Controller类型的bean:
<context:component-scan base-package="com.test">
<context:exclude-filter type="regex" expression="com.test.sso.web.*"/>
</context:component-scan>
这样AOP事务就会起作用了!
use-default-filters="false"这个配置的作用!
因为服务器启动时的加载配置文件的顺序为web.xml---applicationContext-dbaop.xml(Spring 事务的AOP代理的配置文件)---spring-servlet.xml(SpringMVC的配置文件),事务的AOP代理没有配置在SpringMVC配置文件中,从而造成新加载的bean覆盖了老的bean,造成事务失效。只要使用use-default-filters=“false”禁用掉默认的行为就可以了。
参考文章: http://www.iteye.com/topic/1072244
http://www.iteye.com/topic/1128522
http://panyongzheng.iteye.com/blog/1477691