声明式事务管理(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)的更多相关文章

  1. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  2. spring事物配置,声明式事务管理和基于@Transactional注解的使用

    http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...

  3. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  4. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  5. spring boot中的声明式事务管理及编程式事务管理

    这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...

  6. spring事务配置,声明式事务管理和基于@Transactional注解的使用(转载)

    原文地址:http://blog.csdn.net/bao19901210/article/details/41724355 事务管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的 ...

  7. spring学习笔记(22)声明式事务配置,readOnly无效写无异常

    在上一节内容中.我们使用了编程式方法来配置事务,这种优点是我们对每一个方法的控制性非常强.比方我须要用到什么事务,在什么位置假设出现异常须要回滚等.能够进行非常细粒度的配置.但在实际开发中.我们可能并 ...

  8. 八、Spring之深入理解声明式事务

    Spring之深入理解声明式事务 何为事务? 事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用. 事务的四个属性: 1.原子性(atomicity) 事务是原子性操 ...

  9. spring基于xml的声明式事务控制配置步骤

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. spring学习08(声明式事务)

    11.声明式事务 11.1 回顾事务 事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎! 事务管理是企业级应用程序开发中必备技术,用来确保数据的完整性和一致性. 事务就是把一系列的动作当 ...

随机推荐

  1. LeetCode--字符串

    1.给定字符串s,分区s使得分区的每个子字符串都是回文. 返回s的所有可能的回文分区.例如,给定s =“aab”,返回 [ ["aa","b"], [" ...

  2. jade-包含

    模板继承是子文件,父文件继承和代码复用的问题,那模版包含是文件与文件之间,文件与区块之间,这种区块内嵌的东西 继承的关键字是extends, 那模板包含使用的是include这个关键字 head.ja ...

  3. Unknown initial character set index '255' received from server. Initial client character 解决方法

    Unknown initial character set index '255' received from server. Initial client character set can be ...

  4. Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket'

    Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket' Disconnected from t ...

  5. ansible中roles的简单使用

    一.初识roles 上节中我们已经了解了playbook,详见  https://www.cnblogs.com/wangtaobiu/p/10691689.html 当我们在配置playbook时会 ...

  6. 关于webpack require.context() 的那点事

    先说 webpack里面有这么一招:使用require.context()方法来自动导入模块 官方文档有点深奥,老衲百度下拿了一段来直接使用,但是想看下它是如何运行的 本篇这里不会有太深入的研究,只是 ...

  7. 2019HDU多校第四场 K-th Closest Distance ——主席树&&二分

    题意 给定 $n$ 个数,接下来有 $q$ 次询问,每个询问的 $l, r, p, k$ 要异或上一次的答案,才是真正的值(也就是强制在线).每次询问,输出 $[l, r]$ 内第 $k$ 小的 $| ...

  8. C# Contract诊断

    命名空间 : using System.Diagnostics.Contracts; 属性标记 : [ContractOption(category: "runtime", set ...

  9. win10 UWP 动画

    原文:win10 UWP 动画 本文告诉大家如何写同一个简单的动画. 动画入门 本文开始写一个简单的动画,只是移动矩形作为本文的例子. 在 UWP 移动元素的动画,可以使用 RenderTransfo ...

  10. Appium自动化测试教程-自学网-monkey简介

    Monkey简介 在Android的官方自动化测试领域有一只非常著名的“猴子”叫Monkey,这只“猴子”一旦启动,就会让被测的Android应用程序像猴子一样活蹦乱跳,到处乱跑.人们常用这只“猴子” ...