转账案例环境搭建

1.引入JAR包

IOC的6个包

AOP的4个包

C3P0的1个包

MySQL的1个驱动包

JDBC的2个目标包

整合JUnit测试1个包

2.引入配置文件

log4j.properties+applicationContext.xml

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout
<?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">

    <!-- C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/>
        <property name="user" value="root"/>
        <property name="password" value="toor"/>
    </bean>

</beans>

3.创建对应的包结构

public interface AccountDao {

    /**
     * 转出
     * @param out
     * @param money
     */
    public void outMoney(String out,double money);

    /**
     * 转入
     * @param in
     * @param money
     */
    public void inMoney(String in,double money);

}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    //转出
    @Override
    public void outMoney(String out, double money) {
        // TODO Auto-generated method stub
        this.getJdbcTemplate().update("update t_account set money=money-? where name=?", money,out);
    }

    //转入
    @Override
    public void inMoney(String in, double money) {
        // TODO Auto-generated method stub
        this.getJdbcTemplate().update("update t_account set money=money+? where name=?", money,in);
    }

}
public interface AccountService {

    /**
     * 转账
     * @param out
     * @param in
     * @param money
     */
    public void pay(String out,String in,double money);

}
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void pay(String out, String in, double money) {
        // TODO Auto-generated method stub

        //转出
        accountDao.outMoney(out, money);

        //异常
        //int i = 1/0;

        //转入
        accountDao.inMoney(in, money);
    }

}

4.修改配置文件

    <!-- 管理业务层 注入持久层对象-->
    <bean id="accountService" class="com.spring.demo1.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!-- 管理持久层 注入数据源(由于继承JdbcDaoSupport,自动生成JDBC模板类)-->
    <bean id="accountDao" class="com.spring.demo1.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

5.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
public class Demo1 {

    @Resource(name="accountService")
    private AccountService accountService;

    @Test
    public void m01(){
        accountService.pay("测试1", "测试2", 1000);
    }

}

不出现异常正常转账

出现异常不回滚

1.XML方式

1.配置事务管理器

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

2.配置事务增强

    <!-- 配置事务增强 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes >
            <tx:method name="pay"/>
        </tx:attributes>
    </tx:advice>

  name="pay" 代表切入点

4.配置AOP切面

    <!-- 配置AOP切面 -->
    <aop:config>
        <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.spring.demo2.AccountServiceImpl.pay(..))"/>
    </aop:config>

  注意:如果是自己编写的切面,使用<aop:aspect>标签,如果是系统制作的,使用<aop:advisor>标签。

5.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext2.xml")
public class Demo1 {

    @Resource(name="accountService")
    private AccountService accountService;

    @Test
    public void m01(){
        accountService.pay("测试1", "测试2", 1000);
    }

}

不出现异常正常转账

出现异常回滚

2.注解方式

1.配置事务管理器

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

2.开启注解事务

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

3.添加注解

在业务层上添加一个注解:@Transactiona

4.测试

不出现异常正常转账

出现异常回滚

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext3.xml")
public class Demo1 {

    @Resource(name="accountService")
    private AccountService accountService;

    @Test
    public void m01(){
        accountService.pay("测试1", "测试2", 1000);
    }

}

【核心核心】10.Spring事务管理【TX】XML+注解方式的更多相关文章

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

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

  2. Spring事务管理的四种方式(以银行转账为例)

    Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败.   原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...

  3. 事务管理(下) 配置spring事务管理的几种方式(声明式事务)

    配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...

  4. Spring事务管理之几种方式实现事务

    1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  5. Spring事务管理之几种方式实现事务(转)

    一:事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  6. Spring事务管理的xml方式

    一个业务的成功: 调用的service是执行成功的,意味着service中调用的所有的dao是执行成功的.  事务应该在Service层统一控制. 如果手动去实现,则需要对dao进行代理,在方法前后进 ...

  7. spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate

    什么是事务,很通俗的话来说就是,我们日常生活中总会出现在银行转账的业务,加入A向B转账100元,此时A的账户中应该减少100元,B的账户中增加100元,但是如果在A转完账B还没有接受的时候,服务器出现 ...

  8. Spring 事务管理tx,aop

    spring tx:advice事务配置 2016年12月21日 17:27:22 阅读数:7629 http://www.cnblogs.com/rushoooooo/archive/2011/08 ...

  9. 9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)

    使用xml的方式进行声明式的事务管理 推荐使用xml的方式,因为可以同时为多个方法进行声明 <!-- 开启Spring中的事务管理(声明式的事务管理) xml--> <!-- 不管是 ...

随机推荐

  1. 关于对现阶段vue项目的一些总结和感想

    一.前言 现阶段手上vue的项目差不多快完了,空闲之余回反复对整个项目的代码结构.实现细节以及框架上的做了一些思考和优化.下面打算把想到的和重点实现的方法记录一下. 二.回顾 对于常规操作,这里不做过 ...

  2. 打包的@font-face包

    在网页中使用 @font-face 规则嵌入字体,前提是可以从你的网站或第三方 Web 服务器下载到相应的字体.以这种方式提供的字体,会在使用该字体的页面第一次加载时被浏览器下载并缓存起来,以后就不用 ...

  3. 本地 win7 与虚拟机Centos7 ping互通和Centos7 上网设置

    VM 12 安装虚拟机我就不表示了 很简单网上找找 一 .虚拟机设置 1.修改使用了 VMWare 12 虚拟机,Oracle VM 用得有点晕 2.配置VM 的NET环境 3.在VMware虚拟机任 ...

  4. Southern African 2001 Stockbroker Grapevine /// Floyd oj1345

    题目大意: 输入n 接下来n行 每行输入m 接下来m对a,b 若干个人之间会传播谣言,但每个人传播给其他人的速度都不一样, 问最快的传播路线(即耗时最短的)中最耗时的一个传播环节. 如果其中有人不在这 ...

  5. 更改idea启动内存信息

    1.到idea的安装目录的bin下,找idea64.exe.vmoptions 文件 2.更改参数 对应的参数解释: -Xms1024m    设置IDEA初时的内存大小,提高Java程序的启动速度. ...

  6. JAVA API about HTTP 2

    import java.io.IOException; import java.nio.charset.Charset; import java.security.KeyManagementExcep ...

  7. smb中继攻击

    一.NTLM hash 和 Net-NTLM hash 1.客户端向服务器发送一个请求,请求中包含明文的登录用户名.服务器会提前保存登录用户名和对应的密码 hash 2.服务器接收到请求后,生成一个 ...

  8. splice用法

    splice()方法给数组添加内容,返回新的数组 splice()方法替换数组一项内容,返回新的数组 如果添加进数组的元素个数不等于被删除的元素个数,数组的长度会发生相应的改变. 比如:从第 2 位开 ...

  9. Yii2 中使用ts

    在运行环境 vagrant Ubuntu box 中安装 sass ,typescript等 安装需要的软件: sudo su -c "gem install sass" # 可选 ...

  10. [JZOJ3302] 【集训队互测2013】供电网络

    题目 题目大意 给你一个有向图,每个点开始有一定的水量(可能为负数),可以通过边流到其它点. 每条边的流量是有上下界的. 每个点的水量可以增加或减少(从外界补充或泄出到外界),但是需要费用,和增加(减 ...