Spring 声明式事务管理方式
声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码.
1.基于XML配置的声明式事务管理方案(案例)
接口Service
public interface IAccountService {
public void account(String outname,String inname,double money);
}
service实现类
//@Transactional()注解时使用
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
//转账操作的方法
@Override
public void account(String outname, String inname, double money) {
//从outname转出
accountDao.accountOut(outname,money);
int a=10/0;
//从inname转入
accountDao.accountIn(inname,money);
//setter注入
}
Dao接口
public interface IAccountDao { public void accountOut(String outname, double money); public void accountIn(String inname, double money); }
Dao实现类
//import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
//由于JdbcDaoSupport的父类继承了Daosupport,它创建了JdbcTemplate,前提我们先注入一个dataSource
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{
//private JdbcTemplate jdbcTemplate;
//从outname中取钱
@Override
public void accountOut(String outname, double money) {
this.getJdbcTemplate().update("update account set money=money-? where name= ?",money,outname);
}
//向inname中存钱
@Override
public void accountIn(String inname, double money) {
this.getJdbcTemplate().update("update account set money=money+? where name= ?",money,inname);
} }
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class accountServiceTest {
@Autowired
private IAccountService accountService;
@Test
public void test1() {
accountService.account("tom", "fox", 500);
}
}
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 引入外部的properties文件 -->
<context:property-placeholder location="classpath:db.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>
<!-- service -->
<bean id="accountService" class="cn.itcast.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- dao -->
<bean id="accountDao" class="cn.itcast.dao.AccountDaoImpl">
<!-- 当注入dataSource后,底层会自动创建一个JdbcTemplate Template入门案例 -->
<!-- c3p0和DriverManagerDatasource都可以创建datasource -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
name:必须的,对哪些方法进行事务控制
isolation 可选 设置事务隔离级别 默认是DEFAULT
propagation:可选 设置事务传播 默认值 REQUIRED
timeout 可选 超时时间 默认值-1
read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。
rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚
no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚
-->
<tx:method name="account" /> </tx:attributes>
</tx:advice> <!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.service.IAccountService.account(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 开启事务管理器 注解时使用-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
db.properties----------当我们需要频繁修改其中的内容,写到这个配置文件便于管理
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///springtest
jdbc.username=root
jdbc.password=123
Spring 声明式事务管理方式的更多相关文章
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- Spring声明式事务管理(基于XML方式实现)
--------------------siwuxie095 Spring 声明式事务管理(基于 XML 方式实现) 以转账为例 ...
- XML方式实现Spring声明式事务管理
1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...
- Spring声明式事务管理基于@Transactional注解
概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解. 第一种方式我已在上文为大 ...
- Spring声明式事务管理基于tx/aop命名空间
目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...
- spring声明式事务管理详情解析
前沿:通过对spring事务管理有了比较深入学习,本文将不做实例,而是指定具体的类和配置文件进行讲解. 本文内容: 1.了解什么是声明式事务? 2.声明式事务管理分别有哪几种? 3.这几种事务管理之间 ...
- Spring声明式事务管理与配置介绍
转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...
- spring 声明式事务管理
简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...
随机推荐
- 基于memcache的缓存机制的6个指令
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- (转)第二十三节 inotify事件监控工具
第二十三节 inotify事件监控工具 标签(空格分隔): Linux实战教学笔记-陈思齐 原文:http://www.cnblogs.com/chensiqiqi/p/6542268.html 第1 ...
- Apache Beam的目标
不多说,直接上干货! Apache Beam的目标 统一(UNIFIED) 基于单一的编程模型,能够实现批处理(Batch processing).流处理(Streaming Processing), ...
- 024-cxf.xml配置文件模板
版本一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...
- vim配置clojure开发环境备忘录
1 需要使用的插件 vundle 使用教程 http://www.cnblogs.com/respawn/archive/2012/08/21/2649483.html vim-fireplace h ...
- 九度oj题目1181:遍历链表
题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2600 解决:1125 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1 ...
- 用 Python 构建 web 应用
用 Python 构建 web 应用 如果说仅仅要用 Python 构建 web 应用,可以将 socket 连接.HTTP 原始请求和响应格式等涉及网络基础的东西交给现成的库来实现,只需要专注于 w ...
- Jersey统一异常处理
众所周知,java服务提供者提供给服务请求者应该是特定格式的数据,而不能出现异常栈类似信息,那么jersey中,如何添加统一的异常处理呢? 针对jersey启动如果是实现了ResourceConfig ...
- jquery点滴总结
1.empty().remove().detach() empty():只移除了 指定元素中的所有子节点,而留下 了<p></p>,仍保留其在dom中所占的位置. remove ...
- Java 获取当前时间距离当天凌晨的秒数
原文出自:https://blog.csdn.net/seesun2012 在前期项目中遇到一个客户端与服务器间的时间同步问题,需要获取到当前时间与当天凌晨时间距离的秒数,写这篇文章主要是为了总结一下 ...