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 也是代理类生成的

相关推荐
IT_陈寒2 小时前
用了Proxy才发现以前的JavaScript白写了
前端·人工智能·后端
唐青枫3 小时前
别把 ArrayList 当成会自动管理内存的 List:Zig 动态数组从入门到实战
后端
鸿蒙开发3 小时前
我为 HarmonyOS 做了一个统一大模型 SDK:@hmkit/ai 正式开源
后端
猪是念来过倒3 小时前
Semaphore 与 RateLimiter:并发控制双雄详解
后端
掘金者阿豪3 小时前
异构数据同步最怕什么?不是同步慢,而是数据对不上
后端
古法安卓4 小时前
Android-日志系统源码解析
android·java·android studio
程序员cxuan4 小时前
Claude 官方的学习教程,太强了。
人工智能·后端·程序员
老孙讲技术4 小时前
把工地遮挡和掉线接进项目部:setMessageCallback 订 alarm 与 deviceStatus
后端·物联网·音视频开发
老孙讲技术4 小时前
关店后有人进门,监控值班群却没响:用 setMessageCallback 订 human,再用 aiHuman 打开人形
后端·物联网·音视频开发
LEE4 小时前
原来 Claude Code 最厉害的工具,一行代码都不写
前端·后端