Spring事务(2)------事务控制方式(3)声明式事务(XML(tx命名空间管理事务))

环境说明

声明式事务(XML(tx命名空间管理事务))

tx配置

tx:advice
  • 名称: tx:advice

  • 类型:标签

  • 归属: beans标签

  • 作用:专用于声明事务通知

  • 格式:

    <beans>
    	<tx: advice id ="txAdvice" transaction-manager="txManager">
    	</tx:advice>
    </beans>
    
  • 基本属性:

    • id :用于配置aop时指定通知器的id
    • transaction-manager :指定事务管理器bean
tx:attributes
  • 名称: tx:attributes

  • 类型:标签

  • 归属: tx:advice标签

  • 作用:定义通知属性

  • 格式:

    <tx:advice id= "txAdvice" transaction-manager="txManager">
    	<tx: attributes>
    	</tx:attributes>
    </tx:advice>
    
  • 基本属性:

tx:method
  • 名称: tx:method

  • 类型:标签

  • 归属: tx:attribute标签

  • 作用:设置具体的事务属性

  • 格式:

    <tx:attributes>
        <!--所有的操作全部都加读写事务-->
    	<tx:method name="*" read-only=" false" />
        <!--get开头的所有操作全部都加只读事务-->
    	<tx:method name="get*" read only="true" />
    </tx:attributes>
    
  • 说明:

    • 通常事务属性会配置多个,包含1个读写的全事务属性, 1个只读的查询类事务属性
代码实现

将业务层的事务处理功能抽取出来制作成AOP通知类修改为配置文件的形式,也就是TxAdvice这个类和在applicationContext.xml中配置aop的内容全给替换掉

  • 在applicationContext.xml中声明tx的命名空间

    <beans ...
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="...
            http://www.springframework.org/schema/tx
            https://www.springframework.org/schema/tx/spring-tx.xsd
            ...">
    
  • 在applicationContext.xml中进行替换

    • 使用tx命名空间配置事务专属通知类

      <!--定义事务管理的通知类-->
          <tx:advice id="txAdvice" transaction-manager="txManager">
              <!--定义控制的事务-->
              <tx:attributes>
      <!--            所有的操作全部都加读写事务-->
                  <tx:method name="*" read-only="false"/>
      <!--            get开头的所有操作全部都加只读事务-->
                  <tx:method name="get*" read-only="true"/>
                  <!--find开头的所有操作全部都加只读事务-->
                  <tx:method name="find*" read-only="true"/>
              </tx:attributes>
          </tx:advice>
      
    • 使用aop:advisor在AOP配置中引用事务专属通知类

      <aop:config>
      <!--        com.yy.service中所有以service结尾的接口,中的所有方法,
              即监控接口中所有的方法,对业务层进行精准锁定,就不会出现问题了-->
              <aop:pointcut id="pt" expression="execution(* com.itheima.service.*Service.*(..))"/>
              <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
          </aop:config>
      

aop:advice与aop:advisor区别

  • aop:advice配置的通知类可以是普通java对象,不实现接口,也不使用继承关系
  • aop:advisor配置的通知类必须实现通知接口上
    • MethodBeforeAdvice
    • AfterReturningAdvice
    • ThrowsAdvice
tx:method属性

image-20211013125218254

 <tx:method
<!--是哪一个方法-->
                name="transfer"
                read-only="false"
<!--超时时长,-1不限制-->
                timeout="-1"
<!--隔离级别,默认是跟着数据库走-->
                isolation="DEFAULT"
<!--事务中如果出现了后面配置的异常,就不要回滚了 -->
                no-rollback-for=""
            <!--事务中如果出现了这里面配置的异常,一定要回滚了,
            上面的和这个就是设置一个特例情况 -->
                rollback-for=""
<!--            -->
                propagation="REQUIRED"
                />
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

?abc!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值