---
title: "Spring中使用声明式事务"
author: "bolan392"
date: "2010-11-12T16:51:01Z"
tags: []
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2012-03-07T13:57:09Z"
id: 810024
url: "https://bolan392.iteye.com/blog/810024"
---

1.首先，配置事务代理

 

```
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
	<bean id="hibernateTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager"
		autowire="byName" />

	<!-- Transactional proxy for the services -->
	<bean id="baseTxProxy" lazy-init="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		 	<property name="transactionManager">
			<ref bean="hibernateTransactionManager" />
		</property>  
		<property name="transactionAttributes">
			<!-- 此处对需要使用事务管理的方法进行配置 -->
			<props>
				<prop key="updateFrequency">PROPAGATION_REQUIRED</prop>
				<prop key="saveStaff">PROPAGATION_REQUIRED</prop>
				<prop key="saveSchenulingDetailed">PROPAGATION_REQUIRED</prop>
				<prop key="delSchenuling">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
```

 2.对需要事务管理的业务逻辑类配置事务代理

```
<!-- 班次管理 -->
	<bean id="frequencyDAO" class="com.order.cc.fwfm.frequency.dao.FrequencyDAO" autowire="byName"/>
	<bean id="wfmFrequencyEntity" class="com.order.cc.fwfm.frequency.entity.WfmFrequencyEntity" autowire="byName" />
    <!--需要被事务管理的类-->
    <bean id="frequencyService" class="com.order.cc.fwfm.frequency.svc.FrequencyService" autowire="byName" />
    <!--配置业务逻辑类的事务代理-->
    <bean id="frequencyServiceTrans" parent="baseTxProxy">
		<property name="target">
			<ref local="frequencyService" />
		</property>
	</bean>
    <!--让原来Action中的业务逻辑类指向事务代理类-->
	<bean name="/frequencyAction" class="com.order.cc.fwfm.frequency.action.FrequencyAction" autowire="byName" >
		<property name="frequencyService" ref="frequencyServiceTrans" />
	
	</bean>
```