五(二)、spring 宣告式事務xml配置

啄木鳥伍迪發表於2021-11-07

概述:

接著上一節內容,把註解配置@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 ;

相關文章