吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)
在 Spring 中,除了使用基于 XML 的方式可以实现声明式事务管理以外,还可以通过 Annotation 注解的方式实现声明式事务管理。 使用 Annotation 的方式非常简单,只需要在项目中做两件事,具体如下。
1)在 Spring 容器中注册驱动,代码如下所示:
<tx:annotation-driven transaction-manager="txManager"/>
2)在需要使用事务的业务类或者方法中添加注解 @Transactional,并配置 @Transactional 的参数。关于 @Transactional 的参数如图

1. 注册驱动
修改 Spring 配置文件 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: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-2.5.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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:c3p0-db.properties" />
<!-- 配置数据源,读取properties文件信息 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置dao -->
<bean id="accountDao" class="com.mengma.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- 配置service -->
<bean id="accountService" class="com.mengma.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao" />
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注册事务管理驱动 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
上述代码中可以看出,与原来的配置文件相比,这里只修改了事务管理器部分,新添加并注册了事务管理器的驱动。
需要注意的是,在学习 AOP 注解方式开发时,需要在配置文件中开启注解处理器,指定扫描哪些包下的注解,这里没有开启注解处理器是因为在第 33~35 行手动配置了 AccountServiceImpl,而 @Transactional 注解就配置在该类中,所以会直接生效。
2. 添加 @Transactional 注解
修改 AccountServiceImpl,在文件中添加 @Transactional 注解及参数,添加后如下所示。
package com.mengma.service.impl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.mengma.dao.AccountDao;
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public class AccountServiceImpl {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfer(String outUser, String inUser, int money) {
this.accountDao.out(outUser, money);
// 模拟断电
int i = 1 / 0;
this.accountDao.in(inUser, money);
}
}
需要注意的是,在使用 @Transactional 注解时,参数之间用“,”进行分隔。
吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring声明式事务管理(基于Annotation注解方式实现)的更多相关文章
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:继承的应用
class Array{ // 表示数组 private int temp[] ; // 整型数组 private int foot ; // 定义添加位置 public Array(int len) ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类
interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:包装类
public class WrapperDemo01{ public static void main(String args[]){ int x = 30 ; // 基本数据类型 Integer i ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:Object类
class Demo{ // 定义Demo类,实际上就是继承了Object类 }; public class ObjectDemo01{ public static void main(String ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:宠物商店实例分析
interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ; ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:抽象类与接口的应用
abstract class A{ // 定义抽象类A public abstract void print() ; // 定义抽象方法print() }; class B extends A { / ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字
class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:对象的多态性
class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:接口的基本实现
interface A{ // 定义接口A public static final String AUTHOR = "李兴华" ; // 全局常量 public abstract ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:final关键字
final class A{ // 使用final定义类,不能有子类 }; class B extends A{ // 错误,不能被继承 }; class A{ public final void p ...
随机推荐
- netsh命令获取wifi历史连接密码
首先[win+r]快捷键打开运行,输入cmd.或点击左下角win-运行-cmd 1.netsh wlan show profiles //列出所有的ap名称 2.netsh wlan show pro ...
- [Database] MAC MySQL中文乱码问题
1 确保数据库编码设置, 可修改my.cnf mysql> show variables like '%character%'; +--------------------------+---- ...
- mathematica练习程序(第一章 Mathematica的基本量)
虽然过去有用Mathematica解过一些问题,不过对这个语言并没有系统学习过. 所以最近想重新把Mathematica系统的学一遍. 偶然在B站上找到了这样一组教程:https://www.bili ...
- 如何发布composer包
1. 首先要有github仓库(其中必须要有 composer.json 配置文件) 2.关联 github 项目 提交成功 3.设置钩子以便同步更新 https://packagist.org/ab ...
- Java经典算法50道题
[程序1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 月 数量 1 1 2 ...
- SystemVerilog Assertion 设计、调试、测试总结(3)
上两篇主要是讲述断言的概念,基本语法,总结等等 这一篇主要是以PPT的形式展示各个场景下关于断言的应用. 为了在设计中加入断言的功能,因此需要写一个DUT.如下: `define `define fr ...
- ng之邮箱校验
$scope.sendMail = function () { // console.log($scope.inputValue.inputEmail); //校验邮箱格式是否正确 if (!$sco ...
- 个人项目发表到GitHub
个人项目源程序代码发送到github上. 因为之前假期已经注册过了账户,所以只是上传了做过的小账本项目. https://www.cnblogs.com/sdcs/p/8270029.html
- robot_framework + selenium + 上传本地文件+win7 32位
1.下载与安装AutoIt v3 地址链接:http://pan.baidu.com/s/1hqsDFBA,我自己是32位的系统,用这个运行可以 2.安装完成后,如下图所示 3. AutoIt Wi ...
- mysq8设置编码utf8
设置mysql默认编码utf8 以及其他配置 系统:centos7 vi /etc/my.cnf #红色部分如果以存在则在他的下方添加 [mysql] default-character-set=ut ...