Spring--基于AOP实现事务控制

本文介绍了如何在Spring框架中使用AOP实现事务管理,包括回滚事务处理、连接的自动获取与释放。通过`@Before`,`@After`,`@AfterThrowing`等注解,确保业务操作的原子性,并展示了在转账服务中的具体应用。
摘要由CSDN通过智能技术生成

/**

  • 回滚事务

*/

public void rollback(){

try {

connectionUtils.getThreadConnection().rollback();

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 释放连接

*/

public void release(){

try {

connectionUtils.getThreadConnection().close();

connectionUtils.removeConnection();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

配置bean.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:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

aop:config

<aop:pointcut id=“pt1” expression=“execution(* com.ly.service.impl..(…))”/>

<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>

AccountServiceImpl中添加相关的方法:

public void transfer(String sourceName, String targetName, Float money) {

System.out.println(“开始执行。。。”);

//2.执行操作

//2.1.根据名称查询转出账户

Account source=accountDao.findAccountByName(sourceName);

//2.2.根据名称查询转入账户

Account target=accountDao.findAccountByName(targetName);

//2.3.转出账户减钱

source.setMoney(source.getMoney()-money);

int a=1/0;

//2.4.转给账户加钱

target.setMoney(target.getMoney()+money);

//2.5.更新装出账户

accountDao.updateAccount(source);

//2.6.更新转入账户

accountDao.updateAccount(target);

}

测试转账案例:

首先我们先看我们数据库表中的数据:

在这里插入图片描述

执行测试代码:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = “classpath:bean.xml”)

public class AccountServiceTest {

@Autowired

private IAccountService as;

@Test

public void testTransfer(){

as.transfer(“aaa”,“bbb”,100f);

}

}

由于在转账过程中,转出账户后出现了/by zero错误,我们在看一下数据库表有没有发生改变。

在这里插入图片描述

数据表并没有发生改变,实现了回滚。

在这里插入图片描述

使用注解修改银行转账案例:

修改bean.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:context=“http://www.springframework.org/schema/context”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package=“com.ly”></context:component-scan>

aop:aspectj-autoproxy</aop:aspectj-autoproxy>

在AccountServiceImpl中添加相关注解:

在这里插入图片描述

在AccountDaoImpl中添加相关注解:

在这里插入图片描述

在ConnectionUtils中添加相关注解:

在这里插入图片描述

在TransactionManager中添加相关注解:

@Component(“txManager”)

@Aspect

public class TransactionManager {

@Autowired

private ConnectionUtils connectionUtils;

@Pointcut(“execution(* com.ly.service.impl..(…))”)

private void pt1(){}

/**

  • 开启事务

*/

//@Before(“pt1()”)

public void beginTransaction(){

try {

connectionUtils.getThreadConnection().setAutoCommit(false);

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 提交事务

*/

//@AfterReturning(“pt1()”)

public void commit(){

try {

connectionUtils.getThreadConnection().commit();

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 回滚事务

*/

//@AfterThrowing(“pt1()”)

public void rollback(){

try {

connectionUtils.getThreadConnection().rollback();

} catch (SQLException e) {

e.printStackTrace();

}

}

/**

  • 释放连接

*/

//@After(“pt1()”)

public void release(){

try {

connectionUtils.getThreadConnection().close();

connectionUtils.removeConnection();

} catch (SQLException e) {

e.printStackTrace();

}

}

@Around(“pt1()”)

public Object aroundAdvice(ProceedingJoinPoint pjp) {

Object rtValue = null;

try {

//1.获取参数

Object[] args = pjp.getArgs();

//2.开启事务

this.beginTransaction();

//3.执行方法

rtValue = pjp.proceed(args);

//4.提交事务

this.commit();

//返回结果

return rtValue;

} catch (Throwable throwable) {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?

掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

面试是跳槽涨薪最直接有效的方式,马上金九银十来了,各位做好面试造飞机,工作拧螺丝的准备了吗?

掌握了这些知识点,面试时在候选人中又可以夺目不少,暴击9999点。机会都是留给有准备的人,只有充足的准备,才可能让自己可以在候选人中脱颖而出。

[外链图片转存中…(img-TZiTjlO7-1713304378102)]

[外链图片转存中…(img-DsJaQEKU-1713304378102)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 25
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值