Spring 事务AOPxml

Spirng中提供的事务AOP

PlatformTransactionManager 接口提供事务操作的方法

  1. DataSourceTransactionManager: JDBC或者iBatis的事务处理器
  2. ItibernateTransactionManager :Hibernate的事务处理器

Spirng中XML设

头文件设置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

配置事务管理器

     <bean id="dataSourceTransactionManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dateSource"></property>
        </bean>//需要导入spring-jdb架包

配置事务通知

 <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManage">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" read-only="false"/>
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            </tx:attributes>
        </tx:advice>

配置method中name可以使用通配符,是全部,find是以find开头的全部
内有标签
isolation:指定事务隔离级别
propagation:指定传播行为

REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
 SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。 
 MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。 
 REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。 
 NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
 NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。 =
 NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

read-only:用于指定事务是否只读,默认为true为只读
timeout:超时时间,默认-1没有超时设置,单位秒
rollback-for:指定一个异常,出现后回滚
no-rollback-for:除指定的异常,其他异常出现全部回滚

配置切片

        <aop:config>
            <aop:pointcut id="pt1" expression="execution( * com.Service.impl.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
        </aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小学弟QAQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值