javaEE Spring,JDBC事务,管理事务之通过注解配置(重点)

src/applicationContext.xml(Spring配置文件,事务管理):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<!-- 导入beans命名空间(约束) xmlns="http://www.springframework.org/schema/beans"
		导入context命名空间(约束) (读取properties文件) xmlns:context="http://www.springframework.org/schema/context"
		导入tx命名空间(约束) (事务) xmlns:tx="http://www.springframework.org/schema/tx"
	 -->
	
	<!-- 指定spring读取db.properties配置 -->
	<context:property-placeholder location="classpath:db.properties"  />
	
	<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
		<property name="dataSource" ref="dataSource" ></property>
	</bean>
	
	<!-- 开启使用注解管理aop事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 1.连接池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
		<property name="driverClass" value="${jdbc.driverClass}" ></property>
		<property name="user" value="${jdbc.user}" ></property>
		<property name="password" value="${jdbc.password}" ></property>
	</bean>
	
	<!-- 2.Dao -->
	<bean name="accountDao" class="cn.xxx.dao.AccountDaoImpl" >
		<property name="dataSource" ref="dataSource" ></property>
	</bean>
	
	<!-- 3.Service -->
	<bean name="accountService" class="cn.xxx.service.AccountServiceImpl" >
		<property name="ad" ref="accountDao" ></property>
	</bean>  

</beans>

AccountServiceImpl.java(Service层):

package cn.xxx.service;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.xxx.dao.AccountDao;

@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
//通过注解来配置事务 (该类中的所有方法都会应用该事务属性配置)
public class AccountServiceImpl implements AccountService {

	private AccountDao ad ;
	
	@Override
	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	//通过注解来配置事务 (为该方法单独再配置不同的事务属性)(会覆盖类上的注解配置)
	public void transfer(Integer from,Integer to,Double money) {
		//减钱
		ad.decreaseMoney(from, money);
		//int i = 1/0;   // 手动模拟异常
		//加钱
		ad.increaseMoney(to, money);
	}

	public void setAd(AccountDao ad) {
		this.ad = ad;
	}

}

Test.java(测试类):

package cn.xxx.tx;

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 cn.xxx.service.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
	@Resource(name="accountService")
	private AccountService as;
	
	@Test
	public void fun1(){
		
		as.transfer(1, 2, 100d);
		
	}
}

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82080501