Spring AOP事务控制实战指南

Spring AOP控制事务

1 导入依赖

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

配置事务

python 复制代码
<!--    Spring 提供的 JDBC 事务管理器-->
<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource"></property>
    </bean>

配置增强

python 复制代码
<!--    增强要搞的事务  eg事务-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS" />
                //查询单条信息
            <tx:method name="select*" read-only="true" propagation="SUPPORTS" />
                //查询多条信息
            <tx:method name="insert*" />//默认方式
            <tx:method name="delete*"  />
            <tx:method name="update*"  />
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

配置切点

python 复制代码
<!--    切点切面-->
   <aop:config>
<!--       切点-->
        //想要增强的方法
       <aop:pointcut id="pointcut" expression="execution(* com.yunkukukukuku.service.*.*(..))"/>
<!--       切面-->
        //txAdvice 事务的id                            切点 
       <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
   </aop:config>

在测试类当中进行可以看出输出结果

复制代码
userService.getClass()是代理类生成的

如果中间失败则会启动回滚事务

基于注解的AOP控制事务

python 复制代码
 <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource"></property>
    </bean>
        <!-- 开启spring对注解事务的支持,并指定对应的事务管理器 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>

在测试类service当中

@Transactional 是 Spring 的声明式事务注解,作用是:
功能:
标记在 Service 类或方法上,开启事务管理
方法执行成功自动提交事务
方法抛出 RuntimeException 自动回滚事务
保证数据库操作的原子性和一致性

使用的userService 也是代理类生成的

相关推荐
Larcher4 小时前
React Router 不只是页面跳转:从 SPA 路由到权限守卫的完整实践
javascript·后端
Larcher4 小时前
大模型为什么每次回答都不一样?一文搞懂 Temperature、Top K 与 Top P
javascript·后端
LucianaiB6 小时前
毕业老学长给我留的最后一句话是:“AI 写的,别挂我名。” 我直接 神(Seed Evolving) 来!助我!
后端
IvanCodes6 小时前
RAG 实战教程(一):RAG 工作原理与完整流程——分片、索引、召回、重排和生成
人工智能·后端·agent
·薯条大王7 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
CodeSheep7 小时前
稚晖君公司人事大变动,来了!
前端·后端·程序员
小满zs7 小时前
Go语言第八章(函数)
后端·go
stark张宇9 小时前
实战Go高级特性:Context超时控制、defer资源回收与Channel通信的关键避坑点
后端·go
ZJH__GO10 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆10 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程