声明式事务管理,基于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 声明式事务管理方式的更多相关文章

  1. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  2. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  3. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  4. XML方式实现Spring声明式事务管理

    1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...

  5. Spring声明式事务管理基于@Transactional注解

    概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解.         第一种方式我已在上文为大 ...

  6. Spring声明式事务管理基于tx/aop命名空间

    目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...

  7. spring声明式事务管理详情解析

    前沿:通过对spring事务管理有了比较深入学习,本文将不做实例,而是指定具体的类和配置文件进行讲解. 本文内容: 1.了解什么是声明式事务? 2.声明式事务管理分别有哪几种? 3.这几种事务管理之间 ...

  8. Spring声明式事务管理与配置介绍

    转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...

  9. spring 声明式事务管理

    简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...

随机推荐

  1. CSAPP阅读笔记-gcc常用参数初探-来自第三章3.2的笔记-P113

    gcc是一种C编译器,这次我们根据书上的代码尝试着使用它. 使用之前,先补充前置知识.编译器将源代码转换为可执行代码的流程:首先,预处理器对源代码进行处理,将#define指定的宏进行替换,将#inc ...

  2. crontab -e

    crontab -e可以配置定时任务 0 */3 * * * cd /root/find && nohup qbittorrent-nox --webui-port=7070 & ...

  3. (Frontend Newbie)JavaScript基础之函数

    函数可以说是任何一门编程语言的核心概念.要能熟练掌握JavaScript,对于函数及其相关概念的学习是非常重要的一步.本篇从函数的基本知识.执行环境与作用域.闭包.this关键字等方面简单介绍Java ...

  4. Pygame 加载音频

    Python Learning:Pygame 加载音频 Python 中自带的 winsound 模块 winsound 模块中 Beep 方法可以调用系统的蜂鸣器,接受一个为 frequency 的 ...

  5. 05.if结构

    分支结构:if  if-else 选择结构:if else-if switch-case 循环结构:while do-while for foreach if语句 语法: if(判断条件) { //要 ...

  6. 【MSDN】 SqlServer DBCC解析

    汇总学习下SqlServer的DBCC指令. DBCC:Transact-SQL 编程语言提供 DBCC 语句以作为 SQL Server 的数据库控制台命令. 数据库控制台命令语句可分为以下类别. ...

  7. JQuery.iviewer

    from: http://test.dpetroff.ru/jquery.iviewer/test/# jquery.iviewer.js: /* * iviewer Widget for jQuer ...

  8. 10th week task -3 Arrow function restore

    Arrow function restore 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { r ...

  9. Aligning Plots in a Column作图列对齐

    Plot[Sin[x], {x, 0, Pi}] Plot[10000 Sin[x], {x, 0, Pi}] 直接作图左边无法对齐,影响图的美观.可以使用左边界空格实现列对齐,代码如下: optio ...

  10. NGUI动态字体的使用

    LZ今年六月刚刚毕业,在学校跟着老师做Android,OpenGL ES方面的项目(说白了就是干苦力╮(╯_╰)╭).年后来SZ了,就业前景好像并没有电视上渲染的那样糟糕,至少IT行业是这样吧,只要你 ...