Spring事务管理3----声明式事务管理(1)
声明式事务管理(1)基于 基于 tx/aop
这种事务管理相比编程式事务管理来说对业务层基本没有改动,通过 TransactionProxyFactoryBean 创建业务层的代理,通过AOP技术给业务层的代理增强,增加事务管理
但是这种方式的事务管理方式组要对每一个业务层的类都创建一个代理,增加了XML的复杂程度。
dao层
/**
* @author AT
* 转账dao层
*/
public interface AccountDao {
/**
* 转出钱
* @param outer
* @param money
*/
public void remove(String outer,Double money);
/**
* 转入钱
* @param input
* @param money
*/
public void add(String input,Double money);
}
dao层实现类
/**
* 转账dao层实现
*/
public class AccountDaoimpl extends JdbcDaoSupport implements AccountDao{
/**
* 转出钱
* @param outer
* @param money
*/
@Override
public void remove(String outer, Double money) {
String sql = "update account set money = money - ? where name = ?";
this.getJdbcTemplate().update(sql, money,outer);
}
/**
* 转入钱
* @param input
* @param money
*/
@Override
public void add(String input, Double money) {
String sql = "update account set money = money + ? where name = ?";
this.getJdbcTemplate().update(sql, money,input);
} }
service业务层实现类
/**
* @author AT
* 编程式事务管理
*/
public class AccountServiceImpl implements AccountSevice { @Resource(name="accountDao")
private AccountDao accountDao;
@Override
public void transfer(final String input, final String out,final Double money) {
accountDao.remove(out, money);
int a = 1/0;
accountDao.add(input, money);
} public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
}
service业务层
/**
* @author AT
* 转账业务接口
*/
public interface AccountSevice {
public void transfer(String input,String out,Double money);//消费
}
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"
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/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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- dao层和业务的类 -->
<bean id="accountDao" class="com.sdf.spring01.AccountDaoimpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="accountService" class="com.sdf.spring01.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!-- 配置业务层的代理 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置目标对象 -->
<property name="target" ref="accountService"/>
<!-- 注入事务管理类 -->
<property name="transactionManager" ref="transactionManager"/>
<!-- 注入事务属性 -->
<property name="transactionAttributes">
<props>
<!--
prop的模式
* PROPAGATION :事务的传播行为
* ISOLATION :事务的隔离级别
* readOnly :只读(不允许执行del uodate modify sql语句)
* -Exception :发生该异常回滚
* +Exceeption :发生该异常不回滚
-->
<prop key="transfer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
测试类
/**
* @author AT
* 测试转账信息 声明式事务管理 基于 tx/aop
* 创建业务层代理 给代理增强
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext01.xml")
public class AccountTest {
// @Resource(name="accountService")
// private AccountSevice accountService;
/**
* 注入代理类
*/
@Resource(name="accountServiceProxy")
private AccountSevice accountService;
@Test
public void test01(){
accountService.transfer("A", "B", 300d);
}
public void setAccountService(AccountSevice accountService) {
this.accountService = accountService;
}
}
Spring事务管理3----声明式事务管理(1)的更多相关文章
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring编程式事务管理及声明式事务管理
本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...
- spring boot中的声明式事务管理及编程式事务管理
这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...
- spring事务配置,声明式事务管理和基于@Transactional注解的使用(转载)
原文地址:http://blog.csdn.net/bao19901210/article/details/41724355 事务管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的 ...
- spring学习笔记(22)声明式事务配置,readOnly无效写无异常
在上一节内容中.我们使用了编程式方法来配置事务,这种优点是我们对每一个方法的控制性非常强.比方我须要用到什么事务,在什么位置假设出现异常须要回滚等.能够进行非常细粒度的配置.但在实际开发中.我们可能并 ...
- 八、Spring之深入理解声明式事务
Spring之深入理解声明式事务 何为事务? 事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用. 事务的四个属性: 1.原子性(atomicity) 事务是原子性操 ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring学习08(声明式事务)
11.声明式事务 11.1 回顾事务 事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎! 事务管理是企业级应用程序开发中必备技术,用来确保数据的完整性和一致性. 事务就是把一系列的动作当 ...
随机推荐
- Linux学习笔记(七)Linux常用命令:挂载命令
一.查询与自动挂载 mount 查询系统中以及挂载的设备 mount -a 依据配置文件 etc/fstab的内容,自动挂载 二.挂载命令 特殊选项 三.挂载光盘 光盘的设备名是默认已知的,为sr0 ...
- Vant UI 组件库如何做rem适配?
Vant是一款移动端基于vue的组件库,V2.1.1版本非常棒.文档地址:https://youzan.github.io/vant/?source=vuejsorg#/zh-CN/intro,那么V ...
- python学习之列表,元祖,字典
python基础 列表 [ ] stats =[1,2,3,4,5] 元素1,2,3,4,5 下标,索引,角标,初始从0开始 增加 stats.append(7)#append方法添加 stats. ...
- 开发神技能 | Python Mock 的入门
Mock是什么 Mock这个词在英语中有模拟的这个意思,因此我们可以猜测出这个库的主要功能是模拟一些东西.准确的说,Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象替代 ...
- JavaScript各种窗口尺寸
浏览器窗口可视区域大小 网页尺寸scrollHeight 网页尺寸offsetHeight
- CentOS7.x安装Wiki.js知识库
近期有个需求,搭建一个知识管理系统,所以就找了几个可作为知识管理的软件,最终定位到wiki.js,之所以选择这个,是看中了它的易部署性,该项目是在2016年12月推出了第一个版本,算是比较新的项目了, ...
- 8、Docker常用安装:tomcat、mysql、redis
1.总体步骤 搜索镜像 拉取镜像 查看镜像 启动镜像 停止容器 移除容器 2.安装tomcat 1.docker hub上面查找tomcat镜像 docker search tomcat 2.从doc ...
- EF非常见错误:EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配
EF非常见错误:EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 问题原因: 两个表A\B之间存在外键关系,当插入表A的时候,A的外键B在B表中不存在可以引起这个问题: ...
- 项目:JS实现简易计算器案例
组件化网页开发下的: 步骤一:让页面动起来的JavaScript深入讲解 的 项目:JS实现简易计算器案例
- 复杂查询 new EntityWrapper<>()
添加查询条件 https://www.cnblogs.com/okong/p/mybatis-plus-guide-one.html (通用) https://www.jianshu.com/p/ce ...