spring事务管理之四:声明式事务管理:使用事务注解的方式

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

事务注解的方式,相对xml的方式来说,有优点,无需配置需要事务的业务方法的切点和事务属性。但是也有缺点,就是需要在每个需要事务的业务方法上加上一个注解@Transaction,否则事务不会生效。

这里沿用之前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>

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 annotation -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</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 org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import com.xxx.springtransaction.dao.AccountDao;
import com.xxx.springtransaction.service.AccountService;
@Transactional
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);
	}

}

测试类:

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);
	}
}

注解式事务管理的重点是:启用事务注解驱动和在业务类中添加事务注解@Transactional

<tx:annotation-transaction transaction-manager="transactionManager"/> 

@Transactional

第一次测试,不使用事务,我们将AccountServiceImpl.java中的导致异常的两行代码注释掉,并且注释@Transactional,然后测试转账,发现是可以成功的。aaa给bbb转账200元,最后aaa的账户800元,bbb的账户1200元。

第二次测试,我们将AccountServiceImpl.java中的导致异常的两行代码去掉注释,然后测试转账,发现是不能全部成功。aaa给bbb转账200元,最后aaa的账户600元,bbb的账户还是1200元,钱转丢了。

第三次测试,将第一次测试中,业务层注释的事务注解@Transactional打开,再次测试转账,aaa向bbb转账200元,结果因为异常,aaa的账户还是600元,bbb的账户还是1200元,没有发生第二步中的钱转丢的情况,因为异常情况下,事务回滚了。所以转账操作一个也没有成功,说明注解式事务生效了。 

对于spring事务管理而言,如果是springmvc的项目,倾向于使用xml配置的方式,如果是springboot了,只能使用注解的方式。

猜你喜欢

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