spring聲明式事務(wù)控制XML配置
spring提供了事務(wù)控制,具體的步驟如下
一、基于XML的聲明式事務(wù)配置的基本步驟
這里先介紹聲明式事務(wù)控制的主要配置步驟,整個(gè)項(xiàng)目的環(huán)境介紹將放在下第二節(jié)介紹
1). 配置事務(wù)管理器
事務(wù)管理器相當(dāng)于代理對象,內(nèi)部封裝了通知方法,需要注入數(shù)據(jù)源(數(shù)據(jù)源的配置在完整項(xiàng)目中介紹)
DataSourceTransactionManager類中有個(gè)屬性nestedTransactionAllowed ,表示允許【嵌套事務(wù)】,基于此原理,然后再基于切面編程(被代理方法前后增強(qiáng)),實(shí)現(xiàn)嵌套事務(wù)!
<!-- 1. 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
2). 配置事務(wù)通知
1.首先得確保導(dǎo)入事務(wù)的約束
- 官網(wǎng)
- Data Access
-
ctrl f搜索 xmlns:tx
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 2. 配置事務(wù)通知 - 指向之前配置的事務(wù)管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
</tx:advice>
3). 配置AOP中通用切入點(diǎn)表達(dá)式并建立事務(wù)通知和切入點(diǎn)表達(dá)式的對應(yīng)關(guān)系
<!-- 配置aop -->
<aop:config>
<!-- 3. 配置切入點(diǎn)表達(dá)式 -->
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!-- 4. 建立切入點(diǎn)表達(dá)式和事務(wù)的對相應(yīng)關(guān)系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
4). 配置事務(wù)的屬性
在事務(wù)的通知tx:advice標(biāo)簽內(nèi)部陪
1.事務(wù)屬性<tx:method ...對應(yīng)的屬性
-
name: 指定切入點(diǎn)方法,屬于之前配置的切入點(diǎn)表達(dá)式;可以使用通配符進(jìn)行匹配, -
isolation: 用于指定事務(wù)的隔離級別。默認(rèn)值是DEFAULT,表示使用數(shù)據(jù)庫的默認(rèn)隔離級別 -
propagation: 用于指定事務(wù)的傳播行為。默認(rèn)值是REQUIRED,表示一定會(huì)有事務(wù),增刪改的選擇。查詢方法可以選擇SUPPORTS. -
read-only: 用于指定事務(wù)是否只讀。只有查詢方法才能設(shè)置為true.默認(rèn)值是false,表示讀寫 -
timeout: 用于指定事務(wù)的超時(shí)時(shí)間,默認(rèn)值是-1,表示永不超時(shí)。如果指定了數(shù)值,以秒為單位。 -
rollback-for: 用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí),事務(wù)回滾;產(chǎn)生其他異常時(shí),事務(wù)不會(huì)滾;沒有默認(rèn)值(設(shè)置值),任何異常都會(huì)回滾。 -
no-rollback-for: 用于指定一個(gè)異常,當(dāng)產(chǎn)生該異常時(shí),事務(wù)不會(huì)滾;產(chǎn)生其他異常時(shí)事務(wù)回滾;沒有默認(rèn)值(設(shè)置值),任何異常都會(huì)回滾
<!-- 2. 配置事務(wù)通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置轉(zhuǎn)賬的事務(wù) -->
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
<!-- 假如,所有的查詢方法都以find開頭,可以如下配置 -->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
二、完整項(xiàng)目代碼
1). 項(xiàng)目結(jié)構(gòu)

spring聲明式事務(wù)控制XML配置項(xiàng)目結(jié)構(gòu).png
2). maven坐標(biāo)
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 切入點(diǎn)表達(dá)式解析的jar包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
3). applicationContext.xml核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置spring的ioc -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring_01"></property>
<property name="username" value="root"></property>
<property name="password" value="abc123"></property>
</bean>
<!-- 1. 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2. 配置事務(wù)通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置轉(zhuǎn)賬的事務(wù) -->
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
<!-- 假如,所有的查詢方法都以find開頭,可以如下配置 -->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 3. 配置切入點(diǎn)表達(dá)式 -->
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!-- 4. 建立切入點(diǎn)表達(dá)式和事務(wù)的對相應(yīng)關(guān)系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
4). Account pojo/javaBean
省略get/set/toString方法; 數(shù)據(jù)庫中對應(yīng)的表為account;對應(yīng)的字段名與該Bean字段名相同。
public class Account {
private Integer id;
private String name;
private Float money;
...
5). dao持久層代碼
import com.itheima.domain.Account;
import java.sql.SQLException;
public interface AccountDao {
/** 根據(jù)id查詢賬戶的余額 */
public Account queryMoney(Integer id) throws SQLException;
/** 根據(jù)id更新賬戶的余額 */
public int updateMoney(Integer id, Float money) throws SQLException;
}
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.sql.SQLException;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
/** @事務(wù)
* 根據(jù)id查詢賬戶的余額,將異常顯示拋出 */
@Override
public Account queryMoney(Integer id) throws SQLException {
return super.getJdbcTemplate().queryForObject("select money from account where id = ?",
new BeanPropertyRowMapper<Account>(Account.class), id);
}
/** @事務(wù)
* 根據(jù)id更新賬戶的余額, 將異常顯示拋出 */
@Override
public int updateMoney(Integer id, Float money) throws SQLException {
return super.getJdbcTemplate().update("update account set money = ? where id = ?",
money, id);
}
}
6). service業(yè)務(wù)層代碼
public interface AccountService {
/** 轉(zhuǎn)賬業(yè)務(wù) */
public boolean transfer(Integer fromId, Integer toId, Float money) throws Exception;
}
package com.itheima.service.impl;
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/** @事務(wù)
* 轉(zhuǎn)賬業(yè)務(wù) */
@Override
public boolean transfer(Integer fromId, Integer toId, Float money) throws Exception {
// 1. 查詢原賬戶余額
Account fromMoney = accountDao.queryMoney(fromId);
if (fromMoney.getMoney() < money) {
throw new RuntimeException("賬戶余額不足,無法完成轉(zhuǎn)賬");
}
// 2. 查詢被轉(zhuǎn)入賬戶的余額
Account toMoney = accountDao.queryMoney(toId);
// 3. 扣除原賬戶的money
accountDao.updateMoney(fromId, fromMoney.getMoney() - money);
// 顯示拋出一個(gè)異常。
int a = 8 / 0;
// 4. 添加被轉(zhuǎn)入賬戶的余額
accountDao.updateMoney(toId, toMoney.getMoney() + money);
return true;
}
}
7). 測試代碼
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyTest {
@Autowired
private ApplicationContext ac;
/** 轉(zhuǎn)賬測試 */
@Test
public void testTransfer() throws Exception {
AccountService service = (AccountService)ac.getBean("accountService");
boolean res = service.transfer(4, 2, 1000F);
System.out.println(res);
}
}