Spring基于注解的AOP事务控制

源码

代码测试

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.zjw</groupId>
<artifactId>day04_eesy_03account_aoptx_anno</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<encoding>UTF-8</encoding>
<spring.version>6.1.1</spring.version>
<jakarta.version>2.1.0</jakarta.version>
<mysql.version>8.0.33</mysql.version>
<dbutils.version>1.7</dbutils.version>
<c3p0.version>0.9.1.2</c3p0.version>
<lombok.version>1.18.30</lombok.version>
<junit.version>4.13.2</junit.version>
<aspectjweaver.version>1.9.21</aspectjweaver.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${dbutils.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies> </project>

Spring配置文件

<?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"> <!--配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.zjw"/> <!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"/> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/eesy_spring?useSSL=false&amp;serverTimeZone=Asia/Shanghai"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean> <!--开启spring对注解AOP的支持-->
<aop:aspectj-autoproxy/>
</beans>

连接数据库工具类

package com.zjw.utils;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component; import javax.sql.DataSource;
import java.sql.Connection; /**
* @author zjw
*/
@Component("connectionUtils")
public class ConnectionUtils {
private ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); @Resource
private DataSource dataSource; public Connection getThreadConnection(){
try {
//1、先从ThreadLocal上获取
Connection conn = tl.get();
//2、判断当前线程上是否有连接
if (conn == null){
//3、从数据源中获取一个连接,并且存入ThreadLocal中
conn = dataSource.getConnection();
tl.set(conn);
}
//4、返回当前线程上的连接
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
} /**
* 把连接和线程解绑
*/
public void removeConnection(){
tl.remove();
}
}

事务管理类,切面类

package com.zjw.utils;

import jakarta.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 和事务管理相关的工具类,它包含了,开启事务,提交事务,回滚事务和释放连接
* @author zjw
*/
@Component("txManager")
@Aspect
public class TransactionManager { @Resource
private ConnectionUtils connectionUtils; @Pointcut("execution(* com.zjw.service.impl.*.*(..))")
private void pt1(){} /**
* 开启事务
*/
// @Before("pt1()")
public void beginTransaction(){
try {
connectionUtils.getThreadConnection().setAutoCommit(false);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 提交事务
*/
// @AfterReturning("pt1()")
public void commit(){
try {
connectionUtils.getThreadConnection().commit();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 回滚事务
*/
// @AfterThrowing("pt1()")
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 释放连接
*/
// @After("pt1()")
public void release(){
try {
connectionUtils.getThreadConnection().close();
connectionUtils.removeConnection();
}catch (Exception e){
e.printStackTrace();
}
} @Around("pt1()")
public Object aroundAdvice(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
//1、获取参数
Object[] args = pjp.getArgs();
//开启事务
this.beginTransaction();
//执行方法
rtValue = pjp.proceed(args);
//提交事务
this.commit();
//返回结果
return rtValue;
}catch (Throwable t){
this.rollback();
throw new RuntimeException(t);
}finally {
//释放资源
this.release();
}
} }

Dao层实现

package com.zjw.dao.impl;

import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import com.zjw.utils.ConnectionUtils;
import jakarta.annotation.Resource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository; import java.util.List; /**
* 账户的持久层实现类
* @author zjw
*/
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao { @Resource
private QueryRunner runner; @Resource
private ConnectionUtils connectionUtils; @Override
public List<Account> findAllAccount() {
try{
return runner.query(connectionUtils.getThreadConnection(),"select * from account",new BeanListHandler<>(Account.class));
}catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public Account findAccountById(Integer accountId) {
try{
return runner.query(connectionUtils.getThreadConnection(),"select * from account where id = ? ",new BeanHandler<>(Account.class),accountId);
}catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void saveAccount(Account account) {
try{
runner.update(connectionUtils.getThreadConnection(),"insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
}catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void updateAccount(Account account) {
try{
runner.update(connectionUtils.getThreadConnection(),"update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public void deleteAccount(Integer accountId) {
try{
runner.update(connectionUtils.getThreadConnection(),"delete from account where id=?",accountId);
}catch (Exception e) {
throw new RuntimeException(e);
}
} @Override
public Account findAccountByName(String accountName) {
try{
List<Account> accounts = runner.query(connectionUtils.getThreadConnection(),"select * from account where name = ? ",new BeanListHandler<>(Account.class),accountName);
if (accounts==null || accounts.size()==0){
return null;
}
if (accounts.size() > 1){
throw new RuntimeException("结果集不唯一,数据有问题");
}
return accounts.get(0);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Service层实现

package com.zjw.service.impl;

import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import java.util.List; /**
* 账户的业务层实现类
* <p>
* 事务的控制应该都在业务层
* @author zjw
*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService { @Resource
private IAccountDao accountDao; @Override
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
} @Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
} @Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
} @Override
public void updateAccount(Account account) {
accountDao.updateAccount(account); } @Override
public void deleteAccount(Integer acccountId) {
accountDao.deleteAccount(acccountId);
} @Override
public void transfer(String sourceName, String targetName, Float money) {
//2、执行操作
//2.1、根据名称查询转出账户
Account source = accountDao.findAccountByName(sourceName);
//2.2、根据名称查询转入账户
Account target = accountDao.findAccountByName(targetName);
//2.3、转出账户减钱
source.setMoney(source.getMoney() - money);
//2.4、转入账户加钱
target.setMoney(target.getMoney() + money);
//2.5、更新转出账户
accountDao.updateAccount(source); int i = 1 / 0; //2.6、更新转入账户
accountDao.updateAccount(target);
}
}

测试

package com.zjw.test;

import com.zjw.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* 使用Junit单元测试:测试我们的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest { @Autowired
private IAccountService as; @Test
public void testTransfer() {
// List<Account> accountList = as.findAllAccount();
// for (Account account : accountList) {
// System.out.println(account);
// }
as.transfer("aaa", "bbb", 100f);
}
}

结果

控制台出现异常,事务回滚

java.lang.RuntimeException: java.lang.ArithmeticException: / by zero

Spring基于注解的AOP事务控制的更多相关文章

  1. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

  2. Spring 基于注解的AOP实现

    在本文开始之前,我要引入一张图,这张图的来源 https://blog.csdn.net/chenyao1994/article/details/79708496 ,版权归原作者所有,我借鉴了原作者的 ...

  3. Spring基于注解配置AOP

    D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml <?xml version="1.0" encoding ...

  4. spring基于注解的事务控制

    pom配置: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  5. 从源码分析 Spring 基于注解的事务

    在spring引入基于注解的事务(@Transactional)之前,我们一般都是如下这样进行拦截事务的配置: <!-- 拦截器方式配置事务 --> <tx:advice id=&q ...

  6. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  7. JAVAEE——spring03:spring整合JDBC和aop事务

    一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...

  8. Spring_Spring与AOP_AspectJ基于注解的AOP实现

    一.AspectJ.Spring与AOP的关系 AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Cl ...

  9. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

  10. Spring基于注解的Cache支持

    Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...

随机推荐

  1. ABB机械手维修37001电机开启接触器错误

    当ABB机器人报告37001电机开启接触器错误时,这往往意味着电机上电的接触器在执行动作时遇到了障碍.具体而言,该错误通常与位于控制柜内左下角的接触器相关,其中K42和K43负责控制电机的开启操作.深 ...

  2. Android:如何在后台启动Activity

    通常我用这段代码开始一个活动: Intent i = new Intent(context, MyActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NE ...

  3. 深入解析:Jupyter Notebook 中魔法命令的使用技巧与应用

    Jupyter Notebook 中的魔法命令为用户提供了诸多便利功能.魔法命令主要分为行魔法(Line magic)和单元魔法(Cell magic),行魔法前缀为"%",单元魔 ...

  4. Java - 高射炮打蚊子(第二弹)

    题记部分 01 || 面试题 001 || 什么是JVM JVM(Java虚拟机)是Java程序运行的环境,它是一个抽象的计算机,包括指令集.寄存器集.堆栈.垃圾回收等.JVM屏蔽了与具体操作系统平台 ...

  5. deepseek-llamafactory模型微调并转为gguf

    模型微调测试 基础设施配置 使用云计算平台 使用vscode进行配置 打开系统盘文件夹 llamafactory基础配置 git clone --depth 1 https://github.com/ ...

  6. ragflow-ollama 知识库建立测试

    ollama查看模型 C:\Users\DK>ollama show deepseek-r1:7b Model architecture qwen2 parameters 7.6B contex ...

  7. QTableView实现在表格内直接对数据库内容进行修改、新增和删除等操作

    文章目录 前言 QSqlTableModel 新增 删除 修改 提交 取消 前言 本文主要利用QSqlTableModel+QTableView来实现直接在QTableView中进行对数据库数据的操作 ...

  8. linux xxx is not in the sudoers file. This incident will be reported.

    前言 linux 报错:xxx is not in the sudoers file. This incident will be reported. 这意味着用户 xxx 没有在 sudoers 文 ...

  9. k8s v1.19版本之后,自签证书过期x509: certificate has expired or is not yet valid

    前言 在 Kubernetes 1.16 版本之前,kubeadm 工具的 alpha certs 子命令用于生成和管理 Kubernetes 集群的证书.然而,从 Kubernetes 1.19 版 ...

  10. Grafana导入 json 文件的 dashboard 错误 Templating Failed to upgrade legacy queries Datasource xxx not found

    前言 编辑或者修改后的 dashboard 保存为 json 文件,在其他环境导入使用,报错 Failed to upgrade legacy queries Datasource xxxxxxx w ...