Spring声明式事物配置不起作用的常见原因

Spring声明式事物配置不起作用的常见原因

<!-- 事务配置: 事务管理器 -->

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>

<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>

<aop:config>
<aop:advisor pointcut="execution(* com.framework.common..*Service*.*(..))"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="get*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="pageQuery" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>


ps:
1、applicationContext.xml 注意启用 <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>
2、applicationContext.xmll注意 expression="execution(* com.xx.xxx.service..*(..)) "的正确书写
3、如果是采用spring mvc方式,注意applicationContext-springmvc.xml文件中<context:component-scan base-package="com.xx.xxx" />可能导致你的Service(此时没有产生事物相关代理类)提前加载,从而导致 事物配置不起作用。

解决方法:

applicationContext-springmvc.xml 添加如下配置,声明spring mvc 初始化时,只扫描controller.

<context:component-scan base-package="com.framework" use-default-filters="false" > 
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 
</context:component-scan>

applicationContext.xmll添加如下配置,声明spring 初始化时,不扫描controller.

<context:component-scan base-package="com.framework"  > 
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> 

</context:component-scan>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值