Spring事务管理4-----声明式事务管理(2)
声明式事务管理 基于AspectJ的 XML 方式配置
通过对事务管理器TransactionManager配置通知(增强),然后再配置切点和切面,详细见applicationContext.xml配置文件
这种事务管理对业务层没有代码修改,在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 interface AccountSevice {
public void transfer(String input,String out,Double money);//消费
}
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;
}
}
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.spring02.AccountDaoimpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="accountService" class="com.sdf.spring02.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!-- 配置事务的通知:(事务的增强) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
* propagation 事务传播行为
* isolation 事务隔离级别
* read-only 只读
* rollback-for 发生异常回滚
* no-rollback-for 发生异常不回滚
* timeout 超时信息
-->
<tx:method name="transfer" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- 配置切面 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.sdf.spring02.*.*(..))" id="pointcut1"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
测试
/**
* @author AT
* 测试转账信息 声明式事务管理 基于AspectJ的 XML 方式配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext02.xml")
public class AccountTest {
@Resource(name="accountService")
private AccountSevice accountService; @Test
public void test01(){
accountService.transfer("A", "B", 300d);
}
public void setAccountService(AccountSevice accountService) {
this.accountService = accountService;
}
}
Spring事务管理4-----声明式事务管理(2)的更多相关文章
- 全面分析 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 回顾事务 事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎! 事务管理是企业级应用程序开发中必备技术,用来确保数据的完整性和一致性. 事务就是把一系列的动作当 ...
随机推荐
- awk 内置函数的使用
转自:http://gdcsy.blog.163.com/blog/static/12734360920130241521280/ 一.split 初始化和类型强制 awk的内建函数sp ...
- 18 Candidates for the Top 10 Algorithms in Data Mining
Classification============== #1. C4.5 Quinlan, J. R. 1993. C4.5: Programs for Machine Learning.Morga ...
- 单元测试框架之unittest(六)
一.摘要 本片博文将介绍unittest框架的一些轻便有效的特性,在我们的测试中经常可以用到 如果有一些测试方法不想执行,如果有些测试方法在某些条件下不执行 该当如何? 如果有些方法未在unittes ...
- 推荐一个.NET(C#)的HTTP辅助类组件--restsharp
互联网上关于.NET(C#)的HTTP相关的辅助类还是比较多的,这里再为大家推荐一个.NET的HTTP辅助类,它叫RestSharp.RestSharp是一个轻量的,不依赖任何第三方的组件或者类库的H ...
- laravel本地开发环境的安装及配置 - Windows:安装 Laravel Homestead 虚拟机
一.安装 VirtualBox-5.2.22-126460-Win.exe 和 vagrant_2.2.2_x86_64.msi(可视化安装包安装); 安装在D盘 二.导入 Homestead Vag ...
- Python中的猴子补丁
monkey patch指的是在运行时动态替换,一般是在startup的时候.用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/s ...
- 谈CSRF与JSONP设置header问题
关于前端发起请求 问题一 JS发起请求的方式 方法一 JS代码中发起请求的方式普遍为AJAX 该技术在 1998 年前后得到了应用.允许客户端脚本发送HTTP请求(XMLHTTP) 方法二 scrip ...
- Web应用运行原理
web应用启动做了什么? 读取web.xml文件 - web.xml常用配置参数: 1).context-param(上下文参数)2).listener(监听器配置参数)3).filter(过滤器 ...
- IO 的底层实现问题
最近在看 JAVA NIO 的相关知识,了解一下IO的底层实现原理. IO涉及到的底层的概念大致如下: 1) 缓冲区操作.2) 内核空间与用户空间.3) 虚拟内存.4) 分页技术. 一,虚拟存储器 虚 ...
- luogu 2515
对于软件的依赖可以转化为图上点之间的边的关系发现对于一个强联通分量内的软件,一安则全安Tarjan缩点缩点后,从虚拟节点 0 向所有入度为 0 的点连边这样就构成了一棵树树形 dp$dp[i][j]$ ...