五(二)、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注解. 显然基于注解的方式更简单易用,更清爽. ...
随机推荐
- Windows Server 2008 系统加固
账号安全:更改管理员账号 更改管理员账户名来避免攻击,提高系统安全性. 以Administrator账户登录本地计算机,开始->运行->compmgmt.msc(计算机管理)->本地 ...
- php环境 安装
php 相关 wget https://www.php.net/distributions/php-7.2.16.tar.gz //你可以官网获取最新的包 tar解压 //一般目录 ...
- Python调用函数带括号和不带括号的区别
1.不带括号时,调用的是这个函数本身 ,是整个函数体,是一个函数对象,不需等该函数执行完成 2.带括号(此时必须传入需要的参数),调用的是函数的return结果,需要等待函数执行完成的结果 如果函数本 ...
- 测试验收标准checklist
需求实现功能清单 功能实现目的 需求改造功能清单 关联功能清单 关联系统 端到端全流程场景 业务联系性场景 业务全流程场景 上下需求关联规则 业务角度在流程中关注项 财报.评级 授信方案 反洗钱 面向 ...
- CF280D-k-Maximum Subsequence Sum【模拟费用流,线段树】
正题 题目链接:https://www.luogu.com.cn/problem/CF280D 题目大意 一个长度为\(n\)的序列,\(m\)次操作 修改一个数 询问一个区间中选出\(k\)段不交子 ...
- P4590-[TJOI2018]游园会【dp套dp】
正题 题目链接:https://www.luogu.com.cn/problem/P4590 题目大意 给出一个长度为\(m\)的字符串\(s\). 对于每个\(k\in[0,m]\)求有多少个长度为 ...
- idea使用gitee的小坑
1. 账号配置 账号配置登陆时提示 *** is not a valid login name: Email support only. 翻译:只能支持邮箱登录 解决方法:在gitee网站上查看自己配 ...
- mysql中一半会选择什么样的字段为索引?(含索引创建删除查看公式)
一.数据量庞大的数据做索引 二.该字段经常出现在where的后面,以条件形式存在,经常被用户搜索的字段 三.很少被增删改的字段,因为增删改后,索引会重新排序 索引的创建 create index 索引 ...
- 5分钟让你掌握Vuex,深入浅出
5分钟让你掌握Vuex,深入浅出 一.什么是Vuex? 概念:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预 ...
- 【UE4 设计模式】状态模式 State Pattern
概述 描述 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类. 其别名为状态对象(Objects for States),状态模式是一种对象行为型模式. 有限状态机(FSMs) ...