一、引入依赖


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.william</groupId><artifactId>spring_day04_01_tx_aop_xml</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version></dependency></dependencies></project>

二、目录结构

三、applicationContext.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:context="http://www.springframework.org/schema/context"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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--开启注解扫描--><context:component-scan base-package="com.william"></context:component-scan><!--创建QueryRunner--><bean id="QueryRunner" class="org.apache.commons.dbutils.QueryRunner"></bean><!--创建dataSource--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!--引入属性文件--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--使用aop解决事务问题--><!--配置切面:切入点,增强--><!--增强:事务管理--><bean id="transactionManager" class="com.william.utils.TransactionManager"></bean><!--aop配置--><aop:config><!--配置切面: ref 关联增强对象--><aop:aspect ref="transactionManager"><!--配置切入点--><aop:pointcut id="pc" expression="execution(* com.william.service.Impl.*.*(..))"></aop:pointcut><!--织入--><aop:before method="beganTransaction" pointcut-ref="pc"></aop:before><aop:after-returning method="Commit" pointcut-ref="pc"></aop:after-returning><aop:after-throwing method="rollBack" pointcut-ref="pc"></aop:after-throwing><aop:after method="release" pointcut-ref="pc"></aop:after></aop:aspect></aop:config>
</beans>

四、Class文件

1.AccountDaoImpl

代码:

package com.william.Dao.Impl;import com.william.Dao.AccountDao;
import com.william.domain.Account;
import com.william.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;import java.sql.SQLException;
import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/5/27  16:00* @description :* @version: 1.0*/
@Repository
public class AccountDaoImpl implements AccountDao {@AutowiredConnectionUtils connectionUtils;@AutowiredQueryRunner queryRunner;@Overridepublic Account findByUserName(String username ) {String sql="select * from account where name = ?";try {return     queryRunner.query(connectionUtils.getThreadConnection(),sql,new BeanHandler<>(Account.class),username);} catch (SQLException e) {e.printStackTrace();}return null;}@Overridepublic void Update(Account account) {String sql =" update account set money = ? where id =?";Object [] params= {account.getMoney(),account.getId()};try {queryRunner.update(connectionUtils.getThreadConnection(),sql,params);} catch (SQLException e) {e.printStackTrace();}}
}

2.AccountServiceImpl

package com.william.service.Impl;import com.william.Dao.AccountDao;
import com.william.domain.Account;
import com.william.service.AccountService;
import com.william.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author :lijunxuan* @date :Created in 2019/5/27  16:14* @description :* @version: 1.0*/
@Service
public class AccountServiceImpl implements AccountService {@AutowiredAccountDao accountDao;@Overridepublic void transfer(String FromMoney, String ToMoney, Float money) {//开启事务:transactionManager//查询账户名称 两个账户名称Account FromuserName = accountDao.findByUserName(FromMoney);Account ToUserName = accountDao.findByUserName(ToMoney);//修改金额FromuserName.setMoney(FromuserName.getMoney()-money);ToUserName.setMoney(ToUserName.getMoney()+money);//int i=1/0;//修改账户表accountDao.Update(FromuserName);accountDao.Update(ToUserName);}
}

3.Utils工具类

(1)ConnectionUtils

package com.william.utils;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27  16:50* @description :* @version: 1.0*/
@Component
public class ConnectionUtils {private ThreadLocal<Connection> t1=new ThreadLocal();@AutowiredDataSource dataSource;/*** 用来获取线程中连接对象* 1, 在第一次访问该方法时, 线程中没有对象,先获取一个,放入线程*/public Connection getThreadConnection(){//获取线程池中的对象Connection conn = t1.get();if (conn==null){try {conn = dataSource.getConnection();t1.set(conn);} catch (SQLException e) {e.printStackTrace();}}return conn;}/*** 移除线程*/public void remove(){t1.remove();}
}

(2)TransactionManager

package com.william.utils;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27  16:50* @description :* @version: 1.0*/
@Component
public class TransactionManager {@AutowiredConnectionUtils connectionUtils;//开启事务public void beganTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();}}//提交事务public void Commit(){try {connectionUtils.getThreadConnection().commit();} catch (SQLException e) {e.printStackTrace();}}//回滚public void rollBack(){try {connectionUtils.getThreadConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}//释放资源public void release(){try {connectionUtils.getThreadConnection().setAutoCommit(true);connectionUtils.getThreadConnection().close();connectionUtils.remove();} catch (SQLException e) {e.printStackTrace();}}
}

4.TestTrasfer测试类

package com.william;import com.william.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author :lijunxuan* @date :Created in 2019/5/27  16:22* @description :* @version: 1.0*/
@Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestTrasfer {@AutowiredAccountService accountService;@Testpublic void testTrasferMoney(){accountService.transfer("william","william-Li",500f);}
}

使用aop解决事务问题(xml版)相关推荐

  1. 使用注解版AOP解决事务问题

    一.注解版和xml版的区别 1. 通知的四种常用类型 (1)aop:before 作用: 用于配置前置通知.指定增强的方法在切入点方法之前执行 属性: method:用于指定通知类中的增强方法名称 p ...

  2. Spring MVC 中使用AOP 进行事务管理--XML配置实现

    1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...

  3. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  4. Spring中解决事务以及异步注解失效

    Spring中解决事务以及异步注解失效 一.重现@Transaction失效的场景 有如下业务场景,新增订单后,自动发送短信,下面的代码在同一个类中: @Transaction public void ...

  5. Spring5-IOC、AOP、事务管理

    笔记整理来源于尚硅谷,仅供本人复习使用,无其它目的 一.Spring框架概述 二.IOC容器 1.IOC底层原理 (1)IOC定义 ①控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理 ...

  6. 注解mysql事物管理_Spring事务管理-注解版

    Spring事务管理-注解版 一.拷贝必要的jar包到工程的lib目录 二.创建spring的配置文件并导入约束 xmlns:xsi="http://www.w3.org/2001/XMLS ...

  7. spring4声明式事务—02 xml配置方式

    1.配置普通的 controller,service ,dao 的bean. <!-- 配置 dao ,service --><bean id="bookShopDao&q ...

  8. spring控制事务:声明式事务(XML)事务的传播行为

    声明式事务(XML) 使用spring提供的专用于mybatis的事务管理器在xml中声明式事务 声明式事务需要使用到的标签 tx配置 进行<tx 标签的使用需要在xml头部导入命名空间 xml ...

  9. AspectJ注解版和XML版

    什么是AspectJ? AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. Aspec ...

最新文章

  1. css中图片整合的使用,CSS Sprites:图片整合技术详细案例
  2. C/C++如何整行读入字符串?
  3. git clone 时候出现Please make sure you have the correct access rights and the repository exists.
  4. 转载:AD的授权还原和主还原:深入浅出Active Directory系列(六)
  5. c语言程序设计2试卷答案,《C语言程序设计》试卷2参考答案.doc
  6. 关于算法--蛮力法篇--选择排序
  7. HBase入门: 简介、特点、优缺点、数据结构、系统架构、入门操作、适用场景、注意事项与遇到的坑
  8. 条码打印软件如何设置双排标签纸尺寸 1
  9. 你已经是智能机器人,该上岗新基建了
  10. 内联函数有什么优点?内联函数和宏定义的区别
  11. [Alpha] Scrum Meeting 6 - TEAM LESS ERROR
  12. CUDA 深入浅出谈
  13. vb.net 教程 5-12 绘图实例之统计图1
  14. 如何对准UBNT airFiber网桥天线
  15. C语言计算今天是今年的第几个周几
  16. FPGA笔记之——FPGA浮点运算的实现
  17. php生成密码及密码检验
  18. ClickHouse编程指南之DatabaseEngine和TableEngine
  19. 河南濮阳发生持枪抢劫案 公安部发通缉令缉捕嫌犯
  20. rust怎么拆自己石墙_rust自己家怎么拆墙 | 手游网游页游攻略大全

热门文章

  1. cloud foundry_Cloud Foundry Java客户端–流事件
  2. jcmd 命令_jcmd:一个可以全部统治的JDK命令行工具
  3. sts集成jboss_JBoss BPM Travel Agency演示与现代BPM数据集成
  4. java 科学计数_Java和甜蜜的科学
  5. 使用JSON模式验证来映射稀疏JSON
  6. JDK 13中的JEP 355文本块
  7. undertow服务器分析_使用undertow构建和测试Websocket服务器
  8. Java 11即将发布的功能–启动单文件源程序
  9. Apache Derby数据库用户和权限
  10. 许多参数和丢失的信息