概述:

接着上一节内容,把注解配置@@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配置的更多相关文章

  1. 五(一)、spring 声明式事务注解配置

    一.事务概述: 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用:比如 用户购买图书:购买动作之前需要确认 ①图书的数量是否足够:②用户账号余额是否足够 ...

  2. Spring声明式事务的配置~~~

    /*2011年8月28日 10:03:30 by Rush  */ 环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加 ...

  3. spring声明式事务以及配置

    使用spring提供的事务处理机制的好处是程序员可以不用关心事务的切面了,只要配置就好了,可以少写代码. spring声明式事务处理 spring 声明:针对的是程序员,程序员告诉spring容器,哪 ...

  4. Spring声明式事务的配置方式

    1.事务的特性   原子性:事务中的操作是不可分割的一部分   一致性:要么同时成功,要么同时失败(事务执行前后数据保持一致)   隔离性:并发互不干扰     持久性:事务一旦被提交,它就是一条持久 ...

  5. XML方式实现Spring声明式事务管理

    1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...

  6. JavaEE学习之Spring声明式事务

    一.引言 上一篇文章,学习了AOP相关知识,并做了一个简单的Hello world.本文在上篇文章的基础上,进一步学习下Spring的声明式事务. 二.相关概念 1. 事务(Transaction)— ...

  7. spring 声明式事务管理

    简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...

  8. Spring声明式事务(xml配置事务方式)

    Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...

  9. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

随机推荐

  1. 【PHP】数组按照字母排序

    /** * 将数组按字母A-Z排序 * @return [type] [description] */ private function chartSort($list) { // $user=$th ...

  2. [原创]OpenEuler20.03安装配置PostgreSQL13.4详细图文版

    OpenEuler安装配置PostgreSQL 编写时间:2021年9月18日 作者:liupp 邮箱:liupp@88.com 序号 更新内容 更新日期 更新人 1 完成第一至三章内容编辑: 202 ...

  3. DS博客作业04--图

    这个作业属于哪个班级 数据结构--网络2011/2012 这个作业的地址 DS博客作业04--图 这个作业的目标 学习图结构设计及相关算法 姓名 黄静 目录 0.PTA得分截图 1.本周学习总结 1. ...

  4. P4351-[CERC2015]Frightful Formula【组合数学,MTT】

    正题 题目链接:https://www.luogu.com.cn/problem/P4351 题目大意 \(n*n\)的矩形,给出第一行和第一列的数,剩下的满足\(F_{i,j}=a*F_{i,j-1 ...

  5. P4323-[JSOI2016]独特的树叶【换根dp,树哈希】

    正题 题目链接:https://www.luogu.com.cn/problem/P4323 题目大意 给出\(n\)个点的树和加上一个点之后的树(编号打乱). 求多出来的是哪个点(如果有多少个就输出 ...

  6. idea使用gitee的小坑

    1. 账号配置 账号配置登陆时提示 *** is not a valid login name: Email support only. 翻译:只能支持邮箱登录 解决方法:在gitee网站上查看自己配 ...

  7. CSS 奇技淫巧 | 妙用 drop-shadow 实现线条光影效果

    本文将介绍一种利用 CSS 滤镜 filter 的 drop-shadow(),实现对 HTML 元素及 SVG 元素的部分添加阴影效果,以实现一种酷炫的光影效果,用于各种不同的场景之中.通过本文,你 ...

  8. nodejs 安装 报错解决方案

    win10安装nodejs之后,查看版本号在终端输入node -v成功输出版本号,输入npm -v 之后报错...... 反复安装卸载之后,有点奔溃,最后的解决方案是:手动删除"C:\Use ...

  9. .Net Core 获取上下文HttpContext

    1.先定义一个类 using Microsoft.AspNetCore.Http; namespace BCode.Util { public class MvcContext { public st ...

  10. 题解 最长道路tree

    题目传送门 题目大意 给出一个\(n\)个点的树,每个点有点权,定义一条链的贡献为该链的点数乘上链上的权值和,求出树上所有链中的权值最大值. \(n\le 5\times 10^4\) 思路 算是我入 ...