五(二)、spring 声明式事务xml配置
概述:
接着上一节内容,把注解配置@@Transactional形式改为xml配置形式;
一、配置步骤
1.配置事务管理器
1 <!-- 1配置事务管理器 -->
2 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
3 <property name="dataSource" ref="datasource1"></property>
4 </bean>
2.配置事务属性
1 <!-- 根据方法名,指定事务的属性,若不指定,则方法名用*代替 -->
2 <tx:advice id="txAdive" transaction-manager="transactionManager">
3 <tx:attributes>
4 <tx:method name="purchase" propagation="REQUIRED"/>
5 </tx:attributes>
6 </tx:advice>
3.配置切点
1 <!-- 3配置事务切点 -->
2 <aop:config>
3 <aop:pointcut expression="execution(public void lixiuming.spring.tx.xml.service.*.*(..))" id="txpointcut" />
4 <aop:advisor advice-ref="txAdive" pointcut-ref="txpointcut"/>
5 </aop:config>
execution(public void lixiuming.spring.tx.xml.service.*.*(..)) 的说明 ,详见三(二)、AOP配置
附上xml文件:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11
12
13 <!-- 配置测试ContactsDao -->
14 <!-- <context:component-scan base-package="lixiuming.spring.jdbc"></context:component-scan> -->
15 <context:component-scan base-package="lixiuming.spring.tx.xml"></context:component-scan>
16
17 <!-- 导入资源文件 -->
18 <context:property-placeholder location="classpath:db.properties"/>
19 <!-- 配置c3p0数据源 -->
20 <bean id="datasource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
21 <property name="user" value="${jdbc.user}"></property>
22 <property name="password" value="${jdbc.password}"></property>
23 <property name="driverClass" value="${jdbc.driverClass}"></property>
24 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
25
26 <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
27 <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
28 <property name="maxStatements" value="${jdbc.maxStatements}"></property>
29 </bean>
30
31
32 <!-- 配置 NamedParameterJdbcTemplate 该对象可以使用具名参数 他没有无参数的构造器,必须指定构造器参数-->
33 <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
34 <constructor-arg ref="datasource1"></constructor-arg>
35 </bean>
36
37 <!--配置spring的 jdbcTemplate -->
38 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
39 <property name="dataSource" ref="datasource1"></property>
40 </bean>
41 <!-- 配置bean -->
42 <bean id="bookShop" class="lixiuming.spring.tx.xml.BookShopImpl">
43 <property name="jdbcTemplate" ref="jdbcTemplate"></property>
44 </bean>
45
46 <bean id="bookShopService" class="lixiuming.spring.tx.xml.service.impl.BookShopServiceImpl">
47 <property name="dao" ref="bookShop"></property>
48 </bean>
49
50 <bean id="cashier" class="lixiuming.spring.tx.xml.service.impl.CashierImpl">
51 <property name="service" ref="bookShopService"></property>
52 </bean>
53 <!-- 1配置事务管理器 -->
54 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
55 <property name="dataSource" ref="datasource1"></property>
56 </bean>
57
58 <!--2 配置事务属性 -->
59 <!-- 根据方法名,指定事务的属性,若不指定,则方法名用*代替 -->
60 <tx:advice id="txAdive" transaction-manager="transactionManager">
61 <tx:attributes>
62 <tx:method name="purchase" propagation="REQUIRED"/>
63 </tx:attributes>
64 </tx:advice>
65
66 <!-- 3配置事务切点 -->
67 <aop:config>
68 <aop:pointcut expression="execution(public void lixiuming.spring.tx.xml.service.*.*(..))" id="txpointcut" />
69 <aop:advisor advice-ref="txAdive" pointcut-ref="txpointcut"/>
70 </aop:config>
71
72
73
74 </beans>
4.测试方法:
1 package lixiuming.spring.tx.xml;
2
3 import java.util.Arrays;
4
5 import org.junit.Test;
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 import lixiuming.spring.tx.xml.service.BookShopService;
10 import lixiuming.spring.tx.xml.service.Cashier;
11
12 public class SpringTransactionTest {
13
14 private ApplicationContext cxt = null;
15 private BookShopService parchase = null;
16 private Cashier c = null;
17
18 {
19 cxt = new ClassPathXmlApplicationContext("applicationcontext22.xml");
20 parchase = (BookShopService) cxt.getBean("bookShopService");
21 c = (Cashier) cxt.getBean("cashier");
22 }
23
24 @Test
25 public void testCheckout() {
26 c.checkout("aa", Arrays.asList(1001, 1001));
27
28 }
29
30 @Test
31 public void testpurchase() {
32 parchase.purchase("aa", 1001);
33 }
34
35 }
测试:
测试前提:用户账户表 账户金额为120 ; 书号1001和1002的图书库存为 10 ;购买第一本书时,账户余额是够的,但是第二本书钱不够;
当第一次运行testCheckout时,报错为余额不足; 书号1001和1002的图书库存为 还是为10;用户账户表 账户金额为120 ;
五(二)、spring 声明式事务xml配置的更多相关文章
- 五(一)、spring 声明式事务注解配置
一.事务概述: 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用:比如 用户购买图书:购买动作之前需要确认 ①图书的数量是否足够:②用户账号余额是否足够 ...
- Spring声明式事务的配置~~~
/*2011年8月28日 10:03:30 by Rush */ 环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加 ...
- spring声明式事务以及配置
使用spring提供的事务处理机制的好处是程序员可以不用关心事务的切面了,只要配置就好了,可以少写代码. spring声明式事务处理 spring 声明:针对的是程序员,程序员告诉spring容器,哪 ...
- Spring声明式事务的配置方式
1.事务的特性 原子性:事务中的操作是不可分割的一部分 一致性:要么同时成功,要么同时失败(事务执行前后数据保持一致) 隔离性:并发互不干扰 持久性:事务一旦被提交,它就是一条持久 ...
- XML方式实现Spring声明式事务管理
1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...
- JavaEE学习之Spring声明式事务
一.引言 上一篇文章,学习了AOP相关知识,并做了一个简单的Hello world.本文在上篇文章的基础上,进一步学习下Spring的声明式事务. 二.相关概念 1. 事务(Transaction)— ...
- spring 声明式事务管理
简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...
- Spring声明式事务(xml配置事务方式)
Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
随机推荐
- Java基础系列(12)- 运算符
运算符 算数运算符:+ - * / % ++ -- 赋值运算符:= += -= *= /= 关系运算符:> < >= <= == != instanceof 逻辑运算符:&am ...
- Linux系列(19) - 常用压缩命令(2)
常用压缩格式 .tar.gz .tar.bz2 上述两个原理:先用tar进行打包,打完包再用gz或者bz2进行压缩 打包命令tar 命令格式 tar -cvf [打包文件名] [源文件1] [源文件2 ...
- chrome 的 options 参数
在使用selenium浏览器渲染技术,爬取网站信息时,默认情况下就是一个普通的纯净的chrome浏览器,而我们平时在使用浏览器时,经常就添加一些插件,扩展,代理之类的应用.相对应的,当我们用chrom ...
- sublime text 3 在Windows下配置sublimelinter-php的路径问题
首先用package control安装sublimelinter和sublimelinter-php,然后依次点击菜单preference-package settings-sublimelinte ...
- 一文彻底掌握Apache Hudi异步Clustering部署
1. 摘要 在之前的一篇博客中,我们介绍了Clustering(聚簇)的表服务来重新组织数据来提供更好的查询性能,而不用降低摄取速度,并且我们已经知道如何部署同步Clustering,本篇博客中,我们 ...
- RabbitMQ持久化机制、内存磁盘控制(四)
一.持久化 如果看到这一篇文章的朋友,都是有经验的开发人员,对持久化的概念就不用再做过多的解析了,经过前面的几篇文章,其实不难发现RabbitMQ 的持久化其实就只分交换器持久化.队列持久化和消息持久 ...
- InstallSheild相关
一.关于使用InstallSheild制作安装包的总结. 1.定制化制作需要了解InstallScript语法,相关资料可以去网上查找,后续提供比较好的资料. 2.有些软件运行是需要一些环境的,譬如使 ...
- 洛谷3809 SA模板 后缀数组学习笔记(复习)
其实SA这个东西很久之前就听过qwq 但是基本已经忘的差不多了 嘤嘤嘤 QWQ感觉自己不是很理解啊 所以写不出来那种博客 QWQ只能安利一些别人的博客了 小老板 真的是讲的非常好 不要在意名字 orz ...
- MIPS流水线技术
华中科技大学 - 计算机硬件系统设计 单周期指令运行动态 Instruction Fetch Instruction Decode Execution MEM Write Back 单周期时空图 设耗 ...
- 初学python-day5 集合