Spring的事务管理:声明式事务管理(非切面)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fly910905/article/details/83547565

手动编码方式缺点:

  • * 代码量增加,代码有侵入性.

声明式事务管理:(非切面方式)

  • 基于TransactionProxyFactoryBean.
  • 导入:aop相应jar包.

第一步:注册平台事务管理器:


<!-- 事务管理器 -->
<bean id="transactionManager"     
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
</bean>

第二步:创建业务层代理对象:

<!-- 配置生成代理对象 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 目标对象 -->
<property name="target" ref="accountService"/>
<!-- 注入事务管理器 -->
<property name="transactionManager" ref="transactionManager"/>
<!-- 事务的属性设置 -->
<property name="transactionAttributes">
<props>
<prop key="transfer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

声明式事务管理(非切面)--完整版配置文件

<?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"
       xsi:schemaLocation="
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.xsd">
	<!-- 引入外部属性文件. -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- 业务层类 -->
	<bean id="accountService" class="cn.itcast.spring3.demo2.AccountServiceImpl">
		<!-- 在业务层注入Dao -->
		<property name="accountDao" ref="accountDao"/>
	</bean>
	
	<!-- 持久层类 -->
	<bean id="accountDao" class="cn.itcast.spring3.demo2.AccountDaoImpl">
		<!-- 注入连接池的对象,通过连接池对象创建模板. -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 配置生成代理对象 -->
	<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<!-- 目标对象 -->
		<property name="target" ref="accountService"/>
		<!-- 注入事务管理器 -->
		<property name="transactionManager" ref="transactionManager"/>
		<!-- 事务的属性设置 -->
		<property name="transactionAttributes">[]
			<props>
				<!-- 
					prop格式:
						PROPAGATION,
						ISOLATION,
						readOnly,
						-Exception,
						+Exception
					
					* 顺序:
						传播行为、
						隔离级别、
						事务是否只读、
						发生哪些异常可以回滚事务(所有的异常都回滚)、
						发生了哪些异常不回滚.
				
				 -->
				<prop key="transfer">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
</beans>

 

第三步:编写测试类:

 

***** 千万注意:注入代理对象

@Autowired

@Qualifier("accountServiceProxy")

private AccountService accountService;

package cn.itcast.spring3.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class SpringTest2 {

	@Autowired
	@Qualifier("accountServiceProxy")[]
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 100d);
	}
}

prop格式:

  • PROPAGATION,
  • ISOLATION,
  • readOnly,
  • -Exception,
  • +Exception

 

* 顺序:

  • 传播行为、
  • 隔离级别、
  • 事务是否只读、
  • 发生哪些异常可以回滚事务(所有的异常都回滚)、
  • 发生了哪些异常不回滚.

 

声明式事务管理(非切面)

***** 缺点:就是需要为每一个管理事务的类生成代理.需要为每个类都需要进行配置.

猜你喜欢

转载自blog.csdn.net/fly910905/article/details/83547565
今日推荐