Spring 框架下 事务的配置(复杂)
//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 框架下 事务的配置(复杂)的更多相关文章
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- 深入剖析 RabbitMQ —— Spring 框架下实现 AMQP 高级消息队列协议
前言 消息队列在现今数据量超大,并发量超高的系统中是十分常用的.本文将会对现时最常用到的几款消息队列框架 ActiveMQ.RabbitMQ.Kafka 进行分析对比.详细介绍 RabbitMQ 在 ...
- Spring框架之事务源码完全解析
Spring框架之事务源码完全解析 事务的定义及特性: 事务是并发控制的单元,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务将逻辑相关的一组操作绑定在一 ...
- Spring 框架下 (增 删 改 )基本操作
//applicationContext.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?><be ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring 框架的事务管理
1. Spring 框架的事务管理相关的类和API PlateformTransactionManager 接口: 平台事务管理器(真正管理事务的类); TransactionDefinition 接 ...
- 通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术
通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages( JSP)技术.Velocity.Tiles.iText 和 POI.Spring MVC ...
- Spring框架下Junit测试
Spring框架下Junit测试 一.设置 1.1 目录 设置源码目录和测试目录,这样在设置产生测试方法时,会统一放到一个目录,如果没有设置测试目录,则不会产生测试代码. 1.2 增加配置文件 Res ...
- Spring框架之事务管理
Spring框架之事务管理 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生. 一致性:指事务前后 ...
随机推荐
- 8、sort排序中比较函数的几种应用方式
1.待排序中的元素作数组的下标或map的键值 例题:PAT甲级_1141 PAT Ranking of Institutions #include<bits/stdc++.h> using ...
- Scrapy实战篇(四)爬取京东商城文胸信息
创建scrapy项目 scrapy startproject jingdong 填充 item.py文件 在这里定义想要存储的字段信息 import scrapy class JingdongItem ...
- iframe-父子-兄弟页面相互传值(jq和js两种方法)
参考文章: http://blog.csdn.net/u013299635/article/details/78773207 http://www.cnblogs.com/xyicheng/archi ...
- python:函数初始
一.函数 1.函数初始:函数就是封装一个功能 2.函数名,函数体,关键字,函数的返回值 def 关键字,定义一个函数 my_len 函数名书写规则和变量一样 def 与函数名中间一个空格 函数名(): ...
- iOS上传本地代码到git
1.顾名思义,首先你得注册一个github账户 这个我就不细说了. 2.然后你得创建一个 repository 步骤见下图 3.相当于创建成功 会跳到下图界面 4.一看就很清楚了 create a ...
- 什么是BOM?,什么是DOM? BOM跟DOM之间的关系
什么是BOM? BOM是browser object model的缩写,简称浏览器对象模型.是用来获取或设置浏览器的属性.行为,例如:新建窗口.获取屏幕分辨率.浏览器版本号等. 比如 alert(); ...
- 权限管理demo-获取Spring上下文工具
package com.mmall.common; import org.springframework.beans.BeansException; import org.springframewor ...
- Java 学习 注解
转载:https://blog.csdn.net/briblue/article/details/73824058 注解语法 因为平常开发少见,相信有不少的人员会认为注解的地位不高.其实同 class ...
- 使用Jmeter进行http接口测试(转载)
原文:http://www.cnblogs.com/puresoul/p/4740436.html 前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于 ...
- Spring MVC 之 ContentNegotiatingViewResolver
我们已经知道 对于 RequestMappingInfoHandlerMapping, 它在对带有后缀的http 请求进行匹配的时候,如果找不到精确的pattern, 那么就会 pattern+.* ...