25.Spring-事务管理-XML配置aop事务(重点)

25.Spring-事务管理-XML配置aop事务(重点)

ServiceImpl

package vc.helloworld.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import vc.helloworld.dao.AccountDao;

public class AccountSerivceImpl implements AccountSerivce {
	private AccountDao ad;
	private TransactionTemplate tt;

	@Override
	public void transfer(final Integer from, final Integer to, Double money) {
		
		ad.decreaseMoney(from, money);
		//int i = 1/0 ;
		ad.increaseMoney(to, money);

	}

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

	public void setTt(TransactionTemplate tt) {
		this.tt = tt;
	}

}

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:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-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
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 配置事务通知 "transactionManager"操作事务的核心 -->
	<tx:advice id="txAcvice" transaction-manager="transactionManager">

		<tx:attributes>
		<!-- 以方法为单位,指定方法应用什么事务属性
			isolation:隔离级别
			propagation:传播行为
			read-only:是否只读
		 -->
			<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
			<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
		<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
		<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
		<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
		<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
		<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
		<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
		<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
	<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> 
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置织入 -->
	<aop:config >
	<aop:pointcut expression="execution(* vc.helloworld.service.*SerivceImpl.*(..))" id="txPc"/>
	
	<!-- 配置切面 -->
	<aop:advisor advice-ref="txAcvice" pointcut-ref="txPc"/>
	</aop:config>


	<!-- 执行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>
	<!-- 编码是操作事务,需要使用事务的模板对象 -->
	<bean name="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<!-- 调用事务的相关操作,都封装在这个事务模板里面了 -->
		<!-- 代码操作的时候,只要在Service里面调用TransactionTemplate这个事务的方法,就能把事务加载进去 -->
		<!-- 事务模板对象也是有依赖关系, 就是事务核心管理器 -->
		<property name="transactionManager" ref="transactionManager"></property>

	</bean>



	<!-- 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>

	<!-- Dao -->
	<bean name="accountDao" class="vc.helloworld.dao.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 3.Service -->
	<bean name="accountSerivce"
		class="vc.helloworld.service.AccountSerivceImpl">
		<property name="ad" ref="accountDao"></property>
		<property name="tt" ref="transactionTemplate"></property>
	</bean>
</beans>

Service

package vc.helloworld.service;

public interface AccountSerivce {
	// 转账方法
	void transfer(Integer from, Integer to, Double money);
}

daoImpl

package vc.helloworld.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	@Override
	public void increaseMoney(Integer id, Double money) {
		// 定义个一方法,可以增加金钱的数目
		getJdbcTemplate().update("update t_acount set money = money + ? where id = ?", money, id);
	}

	@Override
	public void decreaseMoney(Integer id, Double money) {
		// 定义个一方法,可以减少金钱的数目
		getJdbcTemplate().update("update t_acount set money = money - ? where id = ?", money, id);
	}

}

dao

package vc.helloworld.dao;

public interface AccountDao {

	 //加钱
	void increaseMoney(Integer id,Double money);
	//减钱
	void decreaseMoney(Integer id ,Double money);
}

测试类

package vc.helloworld.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 vc.helloworld.service.AccountSerivce;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContextShiWu.xml")
public class test {
	@Resource(name = "accountSerivce")
	private AccountSerivce accountSerivce;

	@Test
	public void fun15() {
		accountSerivce.transfer(1, 2, 100d);
	}
}