Spring声明式事务管理(基于XML方式实现)
--------------------siwuxie095
Spring 声明式事务管理(基于 XML 方式实现)
以转账为例
1、基于
XML 配置文件的方式实现声明式事务管理,其思想就是 AOP 思想
2、导入相关
jar 包(共 13 个包)
(1)导入核心 jar 包和日志相关的 jar 包
(2)导入
JdbcTemplate 的 jar 包
(3)导入
MySQL 的 JDBC 驱动包
mysql-connector-java
下载链接:
https://dev.mysql.com/downloads/connector/j/
(4)导入 AOP 和
AspectJ 的 jar 包
其中:
aopalliance
下载链接:
http://mvnrepository.com/artifact/aopalliance/aopalliance
aspectjweaver
下载链接:
http://mvnrepository.com/artifact/org.aspectj/aspectjweaver
3、在
MySQL 中手动创建数据库和表
数据库名:tx_db,表名:account,字段:id、name、money
手动添加数据,用作测试
4、具体步骤
(1)配置事务管理器
<!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 在 DataSourceTransactionManager 源代码中有 属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean> |
(2)配置事务的通知(增强)
<!-- 配置事务的通知(增强) --> <tx:advice <tx:attributes> <!-- isolation="DEFAULT" 隔离级别 propagation="REQUIRED" 传播行为 read-only="false" 只读 timeout="-1" 过期时间 rollback-for="" -Exception no-rollback-for="" +Exception --> <!-- 对进行事务操作的方法(一般是业务层方法)设置匹配规则, 如:transfer* 即 --> <tx:method </tx:attributes> </tx:advice> |
(3)配置
AOP
<!-- 配置 AOP --> <aop:config> <!-- 配置切入点 --> <aop:pointcut <!-- 配置切面(增强 + 切入点) --> <aop:advisor </aop:config> |
5、具体实现
(1)编写一个
Dao 类
AccountDao.java:
package com.siwuxie095.dao; import org.springframework.jdbc.core.JdbcTemplate; public class AccountDao { private JdbcTemplate jdbcTemplate;
public this.jdbcTemplate = jdbcTemplate; }
/** * 转出 */ public String sql="update account set money=money-? where name=?"; jdbcTemplate.update(sql, money, from); }
/** * 转入 */ public String sql="update account set money=money+? where name=?"; jdbcTemplate.update(sql, money, to); }
} |
(2)编写一个
Service 类
AccountService.java:
package com.siwuxie095.service; import com.siwuxie095.dao.AccountDao; public class AccountService { private AccountDao accountDao;
public this.accountDao = accountDao; }
/** * 转账 */ public
accountDao.lessMoney(from, money);
// 即便中间出现了什么异常,也会进行回滚 // 如:int num=10/0;
accountDao.moreMoney(to, money);
}
} |
(3)在配置文件中进行配置
applicationContext.xml:
<?xml <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" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置内置连接池 --> <bean <property <!-- jdbc:mysql:///tx_db 是 jdbc:mysql://localhost:3306/tx_db 的简写 --> <property <property <property </bean>
<!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 在 DataSourceTransactionManager 源代码中有 属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean>
<!-- 配置事务的通知(增强) --> <tx:advice <tx:attributes> <!-- isolation="DEFAULT" 隔离级别 propagation="REQUIRED" 传播行为 read-only="false" 只读 timeout="-1" 过期时间 rollback-for="" -Exception no-rollback-for="" +Exception --> <!-- 对进行事务操作的方法(一般是业务层方法)设置匹配规则, 如:transfer* 即 --> <tx:method </tx:attributes> </tx:advice>
<!-- 配置 AOP --> <aop:config> <!-- 配置切入点 --> <aop:pointcut <!-- 配置切面(增强 + 切入点) --> <aop:advisor </aop:config>
<!-- 配置对象并注入属性 --> <bean <property </bean>
<bean <property </bean>
<bean <!-- 在 JdbcTemplate 源代码中有属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean> </beans> |
(4)编写一个测试类
TestDemo.java:
package com.siwuxie095.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.siwuxie095.service.AccountService; public class TestDmo { /** * 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 的 jar 包) * * 选中方法名,右键->Run As->JUint Test */ @Test public
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService=(AccountService) context.getBean("accountService");
accountService.transfer("小白", "小黑", 1000); }
} |
【made by siwuxie095】
Spring声明式事务管理(基于XML方式实现)的更多相关文章
- Spring声明式事务管理基于@Transactional注解
概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解. 第一种方式我已在上文为大 ...
- Spring声明式事务管理基于tx/aop命名空间
目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- XML方式实现Spring声明式事务管理
1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...
- Spring 声明式事务管理方式
声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例) 接口Service public i ...
- Spring声明式事务管理与配置介绍
转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...
- Spring声明式事务如何选择代理方式?
Spring声明式事务如何选择代理方式 解决方法: 1.基于注解方法: <tx:annotation-driven transaction-manager="txManager&q ...
- spring 声明式事务管理
简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
随机推荐
- 杂项:Juice UI
ylbtech-杂项:Juice UI Juice UI是开源的 WebForms 控件集,是一个功能强大的框架,它可以给ASP .NET开发人员带来丰富的.可以作为易于使用的控件的jQuery UI ...
- [转]常用字符与ASCII代码对照表
常用字符与ASCII代码对照表 为了便于查询,以下列出ASCII码表:第128-255号为扩展字符(不常用) ASCII码 键盘 ASCII 码 键盘 ASCII 码 键盘 ASCII 码 键盘 27 ...
- Lua语言中的__index,__newindex,rawget和rawset
转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...
- UNDO三大作用与一致性读机制浅析
UNDO三大作用1.一致性读(consistent read)2.事务回滚(Rollback Transaction)3.实例恢复(Instance Recovery) 一致性读当会话发出一条SQL查 ...
- python2 与python3中最大的区别(编码问题bytes&str
1,在python2.x 中是不区分bytes和str类型的,在python3中bytes和str中是区分开的,str的所有操作bytes都支持 python2 中 >>> s = ...
- 面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力
作者:Tony Peng 去年,OpenAI 的 1v1 AI 击败了世界顶尖选手 Dendi,OpenAI CTO Greg Brockman 承诺:明年,我们会带着 5v5 的 AI bot 重回 ...
- html_table表格
## `table`表格 表格的常用标签 - `table`表格- `thead`表格头- `tbody`表格主体- `tfoot`表格尾- `th`元素定义表头单元格- `tr`定义表格行- `td ...
- centos7.3给squid搭建代理服务器添加认证apache
证使用浏览器打开 然后输入用户名和密码的方式 所以使用基于web服务的apache的 htpasswd 1 创建用户 设置密码 htpasswd -cd /etc/squid/passwords te ...
- Django---form 详解
Form表单的功能 准备数据.重构数据,以便下一步提交. 为数据创建HTML 表单 接收并处理客户端提交的表单和数据 普通字段详解: class BooleanField(**kwargs): 默认的 ...
- UI5-文档-4.2-Bootstrap
在使用SAPUI5做一些事情之前,我们需要加载并初始化它.加载和初始化SAPUI5的过程称为引导.一旦引导完成,我们只需显示一个警告. Preview An alert "UI5 is re ...