Spring AOP事务简单配置

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="query*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>

<aop:config proxy-target-class="true">
<aop:pointcut id="daoMethod"
expression="execution(* com.xx.xx.dao.impl..*.*(..))"/>
<aop:pointcut id="serviceMethod"
expression="execution(* com.xx.xx.service..*.*(..))"/>
<aop:advisor pointcut-ref="daoMethod" advice-ref="txAdvice"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
</aop:config>

刚开始未加proxy-target-class="true", 启动时候报错:
Bean named 'xxx' must be of type [com.xx.xx.xx.Xxx], but was actually of type [$Proxy73]
注入时候报错,被注入的类(代理过的)不是应该所需的类型,由于提示的这个com.xx.xx.xx.Xxx是一个类,而不是接口(没有采用面向接口编程原则,这里有点不太好),加上proxy-target-class="true"使得spring可以使用cglib对类也可以动态代理,就OK了。
另外,对aop-config中expression表达式进行解释
(* com.xx.xx.dao.impl..*.*(..))
第一个*表示匹配所有类型的返回值
第二个*表示匹配所有的类(前面的..表示impl包及其所有子包)
第三个*表示匹配类中所有的方法
最后括号中..表示匹配方法所有参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值