Spring扫描所有包导致事务失效

如果用注解,扫描所有包,会导致事务不起作用,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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值