五(二)、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注解. 显然基于注解的方式更简单易用,更清爽. ...
随机推荐
- Jmeter扩展组件开发(7) - 自定义java请求的开发
CODE package com.demo;import org.apache.jmeter.config.Arguments;import org.apache.jmeter.protocol.ja ...
- Linux命令进阶篇之二
实验内容: cat :由第一行开始显示文件内容 tac:由最后一行开始显示,有没有发现和cat是反过来写的 more:一页一页的显示内容 less:与more相似, ...
- 鸿蒙内核源码分析(定时器篇) | 哪个任务的优先级最高 | 百篇博客分析OpenHarmony源码 | v31.02
百篇博客系列篇.本篇为: v31.xx 鸿蒙内核源码分析(定时器篇) | 哪个任务的优先级最高 | 51.c.h .o 本篇说清楚定时器的实现 读本篇之前建议先读鸿蒙内核源码分析(总目录)其余篇. 运 ...
- P4831-Scarlet loves WenHuaKe【组合数学】
正题 题目链接:https://www.luogu.com.cn/problem/P4831 题目大意 \(n*m\)的网格上放置\(2n\)个炮,要求互不能攻击. 数据满足\(n\leq m\leq ...
- YbtOJ#893-带权的图【高斯消元,结论】
正题 题目链接:https://www.ybtoj.com.cn/problem/893 题目大意 给出一张\(n\)个点\(m\)条边的无向联通图,每条边正反向各有\(A,B,C\)三种边权. 保证 ...
- Go语言之数组与切片基础
一.数组 数组是同一类型元素的集合,可以放多个值,但是类型一致,内存中连续存储 Go 语言中不允许混合不同类型的元素,而且数组的大小,在定义阶段就确定了,不能更改 1.数组的定义 // 定义一个大小为 ...
- oracle dg failover灾难切换
oracle dg failover灾难切换SQL> alter database recover managed standby database finish force;SQL> a ...
- VS2013的主函数问题
报错如下: 打开属性里面,修改字符集即可
- Redis缓存穿透、缓存击穿、缓存雪崩的介绍及其解决方案
首先,来画一张图了解下缓存处理的流程 一.缓存穿透 描述: 缓存穿透是指缓存和数据库中都没有的数据,而用户不断发起请求查询该数据,导致数据库压力过大. 解决方案: 1.接口校验 如鉴权校验.数据合法性 ...
- Editing Tools(编辑工具)
编辑工具 # Process: 修剪线 arcpy.TrimLine_edit("", "", "DELETE_SHORT") # Proc ...