spring事务管理之三:声明式事务管理:使用xml配置文件的方式

版权声明:欢迎转载 https://blog.csdn.net/feinifi/article/details/85067484

这种方式是开发中常见的一种方式:利用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元,证明事务生效了。

猜你喜欢

转载自blog.csdn.net/feinifi/article/details/85067484