spring tx:advice ...aop:config配置事物

spring来配置事务管理,记住三个参数就可以了

<aop:pointcut>切入点    <aop:advice>需要注入的方法   <aop:advisor> 适配器,把注入的方法注入切入点的位置,是连接pointcut跟advice的工具

下面上实例

<aop:config>
         <aop:pointcut expression="execution(public * com.test.registration.service.*.*(..))"
             id="logPointcut" />
        <aop:advisor pointcut-ref="logPointcut" 
        advice-ref="txAdvice"
        />
     </aop:config>
     <tx:advice id="txAdvice" transaction-manager="txManager">
     	<tx:attributes>
     		<tx:method name="exists" read-only="true"/>
     		<tx:method name="add*" propagation="REQUIRED"/>
     		<tx:method name="modify*" propagation="REQUIRED"/>
     	    <tx:method name="del*" propagation="REQUIRED"/>
     	</tx:attributes>
     </tx:advice>

<aop:pointcut expression="execution(public * com.test.registration.service.*.*(..))"

这句话对切入点执行方法的解析public * com.test.registration.service.*.*(..)

第一个*表示返回值为任意值

第二个*表示在 com.test.registration.service这个包中的任意类

第三个*表示当前类的任意方法(..)表示0个或多个参数

对于上边的实例tx:advice的配置解析事物管理来自txManager 而sessionfactory具体就不在详述了主要是配置数据源跟实体类之类的

tx:attributes里的方法,这里主要对增删改查进行了配置,

有的初学者会问,什么是事物呢,事物简单讲就是对多个方法之类的同时操作,遇到错误同时回滚,在这里是为了避免数据提交,

比如一个方法里写了好多个insert,有一个突然由于一些故障,insert不了了,那么直接插入的话,其结果肯定不是自己想要的了,

数据库里出现了一些脏数据。对于里边还有的参数解析,详见spring api.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值