1. 程式人生 > >spring事務管理之三:宣告式事務管理:使用xml配置檔案的方式

spring事務管理之三:宣告式事務管理:使用xml配置檔案的方式

這種方式是開發中常見的一種方式:利用aop的思想,將需要事務管理的業務方法通過xml配置的方式,將事務管理加在該類的相關方法上。這種方法的優點是,一次xml配置,後期不用關心業務類增加或者減少,通過xml中配置的匹配資訊,會去找業務類所在的包和方法,然後加上事務。

重點是配置<tx:advice>和<aop:config>上。

沿用spring事務管理之前的工程:

pom.xml依賴部分

<dependencies>
    <dependency>
      	<groupId>junit</groupId>
      	<artifactId>junit</artifactId>
      	<version>4.12</version>
      	<scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
    	<artifactId>aspectjweaver</artifactId>
    	<version>1.8.13</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context-support</artifactId>
    	<version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-tx</artifactId>
    	<version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-jdbc</artifactId>
    	<version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-test</artifactId>
    	<version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.38</version>
    </dependency>
  </dependencies>

這裡為了使用spring-aop,需要額外引入aspectjweaver的包。

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///shop?useUnicode=true&useSSL=false
jdbc.username=hadoop
jdbc.password=hadoop

spring.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" 
       class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="username" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="accountDao" class="com.xxx.springtransaction.dao.impl.AccountDaoImpl">
    	<property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="accountService" 
        class="com.xxx.springtransaction.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <bean id="transactionManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--  spring transaction aspectj xml -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>
    		<tx:method name="transfer" propagation="REQUIRED"/>
    	</tx:attributes>
    </tx:advice>
    <aop:config>
    	<aop:pointcut expression="execution(* com.xxx.springtransaction.service..*(..))"
           id="pointcut"/>
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

AccountDao.java

package com.xxx.springtransaction.dao;

public interface AccountDao {
	public void transferIn(String id,Double money);
	public void transferOut(String id,Double money);
	public Double findById(String id);
}

AccountDaoImpl.java

package com.xxx.springtransaction.dao.impl;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.xxx.springtransaction.dao.AccountDao;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {	

	@Override
	public void transferIn(String id, Double money) {
		String sql = "update account set money = money + ? where id = ?";
		getJdbcTemplate().update(sql, money,id);
	}

	@Override
	public void transferOut(String id, Double money) {
		String sql = "update account set money = money - ? where id = ?";
		getJdbcTemplate().update(sql, money,id);
	}
	
	@Override
	public Double findById(String id){
		return null;
	}

}

AccountService.java

package com.xxx.springtransaction.service;

public interface AccountService {
	public void transfer(String out,String in,Double money);
	public Double findById(String id);
}

AccountServiceImpl.java

package com.xxx.springtransaction.service.impl;
import com.xxx.springtransaction.dao.AccountDao;
import com.xxx.springtransaction.service.AccountService;
public class AccountServiceImpl implements AccountService {

	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	@Override
	public void transfer(String out, String in, Double money) {	
		accountDao.transferOut(out, money);
		//int i = 1/0;
		//System.out.println(i);
		accountDao.transferIn(in, money);
	}

	@Override
	public Double findById(String id){
		return accountDao.findById(id);
	}

}

單元測試類:SpringTransactionTest.java

package com.xxx.springtransaction;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.xxx.springtransaction.service.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringTransactionTest {
	@Resource
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("1", "2", 200d);
	}
}

aspectj+xml配置事務的重點在於配置切點:

測試事務生效的方法:

1、在xml配置檔案中,將配置事務的部分註釋,不使用事務,業務方法中,將會導致異常的程式碼註釋,正常轉賬。

// int i = 10;

//System.out.println(i);

aaa向bbb轉賬200元,會成功,此時aaa賬戶變為800元,bbb的賬戶變為1200元。

2、將業務程式碼中異常的部分註釋開啟,再次轉賬。aaa給bbb轉賬200,因為異常,aaa的賬戶變為600元,而bbb的賬戶還是1200,錢轉丟了。

3、將spring.xml中關於事務部分的配置去掉註釋,讓事務生效,然後接著轉賬。aaa向bbb轉賬200元,這時候,因為異常會回滾。這時候aaa的賬戶還是600元,bbb的賬戶還是1200元,證明事務生效了。