SpringAop事务xml实现步骤

1.先在bean里面配置事务管理器。这个可以自己写也可以用Spring的事物管理器

2.配置事务行为

propagation="REQUIRED" 指如果当前有事物就在该事物中执行,如果没有,就开启一个新的事物(增删改查中)

propagation="SUPPORTS" read-only="true"  指如果有就执行该事物,如果没有,就不会开启事物(查询中)

 

3.配置aop通用的切入点表达式aop:pointcut

4.配置一些通知aop:aspect:

  1.   前置通知开启事务
  2. 后置通知提交事务
  3. 异常通知回滚事务
  4. 最终通知释放连接

 <!-- 配置事务管理器-->
    <bean id="txManager" class="com.itheima.utils.TransactionManager">
        <!-- 注入ConnectionUtils -->
        <property name="connectionUtils" ref="connectionUtils"></property>
    </bean>

    <!--配置aop-->
    <aop:config>
        <!--配置通用切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
        <aop:aspect id="txAdvice" ref="txManager">
            <!--配置前置通知:开启事务-->
            <aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before>
            <!--配置后置通知:提交事务-->
            <aop:after-returning method="commit" pointcut-ref="pt1"></aop:after-returning>
            <!--配置异常通知:回滚事务-->
            <aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing>
            <!--配置最终通知:释放连接-->
            <aop:after method="release" pointcut-ref="pt1"></aop:after>
        </aop:aspect>
    </aop:config>
 

发布了15 篇原创文章 · 获赞 5 · 访问量 4467

猜你喜欢

转载自blog.csdn.net/qq_37139773/article/details/102451811