//db.properties配置  src下的文件

jdbc.jdbcUrl=jdbc:mysql:///day43
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

//applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.2.xsd
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

<!--指定读取配置文件 -->
   <context:property-placeholder location="classpath:db.properties"/>
   <!-- 事物核心管管理器  依赖连接词池-->
 <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"></property>
 </bean>
   <!-- 事物模板对象 -->
 <bean name="transactionTemplat" class="org.springframework.transaction.support.TransactionTemplate">
      <property name="transactionManager" ref="transactionManager"></property>
 </bean>
 <!-- 配置事物通知  -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
 </tx:advice>
 <!-- 配置织入  -->
 <aop:config>
 <!-- 切点表达式 -->
     <aop:pointcut expression="execution(* cn.jy.service.*ServiceImp.*(..))" id="txPc"/>
 <!-- 配通知给 切点-->
 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
 </aop:config>
   <!--1 将连接池放进spring容器 -->
   <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
       <property name="driverClass" value="${jdbc.driverClass}"></property>
       <property name="user" value="${jdbc.user}"></property>
       <property name="password" value="${jdbc.password}"></property>
   </bean>
   <!-- 2 将jdbc模板放进连接池 -->
   <!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"></property>
   </bean> -->
   <!-- 3将AccountDaoImp放进连接池 -->
   <bean name="accountDaoImp" class="cn.jy.dao.AccountDaoImp">
           <property name="dataSource" ref="dataSource"></property>
   </bean>
   <!-- 4将userServiceImp放进连接池 -->
   <bean name="accountService" class="cn.jy.service.AccountServiceImp">
           <property name="ad" ref="accountDaoImp"></property>
   </bean>
 </beans>

//AccountDaoImp层

package cn.jy.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImp extends JdbcDaoSupport  implements AccountDao {

@Override
    public void addMoney(Integer id, Double money) {
        getJdbcTemplate().update("update account1 set money=money+? where id=?",money,id);

}

@Override
    public void decreaseMoney(Integer id, Double money) {
           getJdbcTemplate().update("update account1 set money=money-? where id=?",money,id);

}

}
//AccountService层

package cn.jy.service;

import cn.jy.dao.AccountDao;

public class AccountServiceImp implements AccountService {
private AccountDao ad;

public void setAd(AccountDao ad) {
    this.ad = ad;
}

@Override
    public void transfer(Integer from, Integer to, Double money) {
        ad.decreaseMoney(to, money);
        
        //int i=0/0;
        ad.addMoney(from, money);

}

}

//测试

package cn.jy.service;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
 @Resource(name="accountService")
 private AccountService aa;
 @Test
 public void fun1(){
     aa.transfer(1, 2,100d);
 }
 
}

Spring 框架下 事务的配置(复杂)的更多相关文章

  1. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  2. 深入剖析 RabbitMQ —— Spring 框架下实现 AMQP 高级消息队列协议

    前言 消息队列在现今数据量超大,并发量超高的系统中是十分常用的.本文将会对现时最常用到的几款消息队列框架 ActiveMQ.RabbitMQ.Kafka 进行分析对比.详细介绍 RabbitMQ 在 ...

  3. Spring框架之事务源码完全解析

    Spring框架之事务源码完全解析   事务的定义及特性: 事务是并发控制的单元,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务将逻辑相关的一组操作绑定在一 ...

  4. Spring 框架下 (增 删 改 )基本操作

    //applicationContext.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?><be ...

  5. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  6. Spring 框架的事务管理

    1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...

  7. 通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术

    通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages( JSP)技术.Velocity.Tiles.iText 和 POI.Spring MVC ...

  8. Spring框架下Junit测试

    Spring框架下Junit测试 一.设置 1.1 目录 设置源码目录和测试目录,这样在设置产生测试方法时,会统一放到一个目录,如果没有设置测试目录,则不会产生测试代码. 1.2 增加配置文件 Res ...

  9. Spring框架之事务管理

    Spring框架之事务管理 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 一致性:指事务前后 ...

随机推荐

  1. bananapi+OLED做的一个打地鼠游戏,c语言编程

    说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...

  2. beef + msf 实现内网渗透

    在内网渗透方面,最为大众所知道的就是xp系统的ms08067漏洞,通过这个漏洞可以对未打上补丁的xp系统实现getshell, 但是经过笔者发现,这种漏洞攻击在被攻击机开上windows防火墙的时候是 ...

  3. linux怎么样显示命令历史后又显示命令的输入时间

    linux的bash内部命令history就可以显示命令行的命令历史,默认环境执行 history命令后,通常只会显示已执行命令的序号和命令本身.如果想要查看命令历史的时间戳,那么可以执行: 临时显示 ...

  4. C语言数据结构基础学习笔记——C语言基础

    抽象数据类型(ADT)是指一个数学模型以及定义在该模型上的一组操作,通常用(数据对象,数据关系,基本操作集)这样的三元组来表示抽象数据类型. 数据结构是相互之间存在一种或多种特定关系的数据元素的集合, ...

  5. Java 里面各种类型之间的相互转换

    1.整形与字符型之间的数据类型转换: 一.int转换成char有两种方法: ①  是利用char的unicode编码 例:int num1 = 8; char ch1 = (char) (num1 + ...

  6. Android 获取控件滑动速度,速度跟踪器VelocityTracker;

    VelocityTracker 速度跟踪器 在写关于Android滑动的控件,如果用户手指在屏幕上(当前位置 - 起始位置 > 某个数值)就做一个界面切换,但是总感觉太生硬,只有满足上面的条件才 ...

  7. 多线程之sleep和wait的区别

    它们最大本质的区别是:sleep()不释放同步锁,wait()释放同步锁. 还有用法的上的不同是:sleep(milliseconds)可以用时间指定来使他自动醒过来,如果时间不到你只能调用inter ...

  8. RedHat 7.0更新升级openSSH7.4p1

    由于目前服务器上ssh版本较低,存在安全漏洞,需要升级到最新版本. 系统版本:RedHat 7.0 旧openSSH版本:6.4p1 新openSSH版本:7.4p1 升级方式:源码安装 安装操作步骤 ...

  9. maven的单元测试中没有

    原因:BaseTest没有找到单元测试造成的 增加一个空的单元测试 @Testpublic void testNothing(){} 异常现象:在maven项目执行mvn install 或mvn t ...

  10. __iter__ 和 __next__

    class F: def __init__(self,x): self.x = x def __iter__(self): #把对象 变成可迭代对象 return self def __next__( ...