声明式事务管理  基于注解

  在配置文件中需要开启注解驱动<tx:annotation-driven transaction-manager="transactionManager"/>;在业务层类的加上@transactional注解

这种事务管理方式非常简单,但是注加载业务层类中,对于后期的维护没有给予AspectJ方式的事务管理简单。

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
* 编程式事务管理
*
* propagation 事务的传播行为
* isolation 事务的隔离级别
* readOnly 只读
* rollbackFor 发生该异常回滚
* noRollbackFor 发生该异常不回滚
* noRollbackForClassName 发生指定异常不回滚
*/
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,readOnly=false)
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.spring03.AccountDaoimpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="accountService" class="com.sdf.spring03.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean> <!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

测试类

/**
* @author AT
* 测试转账信息 声明式事务管理 基于注解的管理方式
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext03.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事务管理5-----声明式事务管理(3)的更多相关文章

  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. 捷克200套UR51出货新版本FTP问题(FTP主动模式无法正常传输数据问题)

    FTP alg功能 普通NAT实现了对UDP或TCP报文头中的的IP地址及端口转换功能,但对应用层数据载荷中的字段无能为力,在许多应用层协议中,比如多媒体协议(H.323.SIP等).FTP.SQLN ...

  2. C#中[STAThread]的作用

    [STAThread]STAThread:Single Thread Apartment Thread.(单一线程单元线程)[]是用来表示Attributes: [STAThread]是一种线程模型, ...

  3. 《构建之法》——Alpha2项目的测试

    这个作业属于哪个课程 课程的链接 这个作业要求在哪里 作业要求的链接 团队名称 Runningman 这个作业的目标 测试其他组的项目,互相借鉴 作业正文 作业正文 测试人姓名 陈嘉莹 测试人学号 2 ...

  4. temp数据预处理--以24h为周期的序列

    1.按照周期来截取数据 从数据库加载下来的是以5min取一次mean()的列,因此24h应取了24*60/5=288次数据 首先把这8352个数据(最后一个以倒数第二个填充)改成288*30的形式 t ...

  5. C语言calloc()函数:分配内存空间并初始化——stm32中的应用

    经常在代码中看到使用malloc来分配,然后memset清零,其实calloc更加方便,一句顶两句~ 头文件:#include <stdlib.h> calloc() 函数用来动态地分配内 ...

  6. BBS 页面搭建知识点整理

    表关系图及建表 from django.db import models # Create your models here. from django.contrib.auth.models impo ...

  7. 一个项目中:只能存在一个 WebMvcConfigurationSupport (添加swagger坑)

    问题再现: 1.添加了swagger配置,导致接口响应的中文乱码 2.于是又添加了配置解决中文乱码的配置: 问题来了,添加了CharsetConfig 配置后swagger的配置失效了,访问404,搞 ...

  8. csv和xlsx区别

    CSV是文本文件,用记事本就能打开.XLS 是二进制的文件只有用 EXCEL 才能打开: CSV 文件格式只能保存活动工作表中的单元格所显示的文本和数值.数据列以逗号分隔,每一行数据都以回车符结束.如 ...

  9. Codeforces Round #587 (Div. 3) C题 【判断两个矩形是否完全覆盖一个矩形问题】 {补题 [差点上分系列]}

    C. White Sheet There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle ...

  10. 国庆集训Day1

    T1 divide 题意: 有\(n\)个数 \(a_1, a_2,..., a_n\) 有m个数\(b_1, b_2,..., b_n\) 令\(a = a_1\times a_2\,\times ...