---
title: "Spring 配置事务XML "
author: "kidfang"
date: "2009-06-01T16:11:30Z"
tags: []
slug: "spring-xml"
draft: false
lastmod: "2009-06-01T16:11:30Z"
id: 399683
url: "https://kidfang.iteye.com/blog/399683"
---

Spring 配置事务XML

===========================================================

作者: mopdzz(http://mopdzz.itpub.net)  
发表于: 2008.09.03 22:05  
分类: Spring  
出处: http://mopdzz.itpub.net/post/20166/470135  
\---------------------------------------------------------------

如果你使用的是Spring2.0，可以使用下面方法，非常方便，直接对类名称和方法名事务控制

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd](http://www.springframework.org/schema/aop/spring-aop-2.0.xsd)  
[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.0.xsd](http://www.springframework.org/schema/tx/spring-tx-2.0.xsd)"&gt;

&lt;!--加载properties配置文件--&gt;  
&lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;  
&lt;property name="locations"&gt;  
&lt;list&gt;  
&lt;value&gt;/WEB-INF/jdbc.properties&lt;/value&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!--数据源--&gt;  
&lt;bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close"&gt;  
&lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;  
&lt;property name="url" value="${jdbc.url}"/&gt;  
&lt;property name="username" value="${jdbc.username}"/&gt;  
&lt;property name="password" value="${jdbc.password}"/&gt;  
&lt;/bean&gt;

&lt;!--事务管理器--&gt;  
&lt;bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
&lt;property name="dataSource" ref="dataSource"/&gt;  
&lt;/bean&gt;

&lt;!--aop代理，对所有以Logic为后缀文件名的接口类进行代理--&gt;  
&lt;aop:config&gt;  
&lt;aop:advisor pointcut="execution(* \*..\*Dao.\*(..))" advice-ref="txAdvice"/&gt;  
&lt;/aop:config&gt;

&lt;!--数据库事务代理，对类中的指定前缀的方法进行事务控制--&gt;  
&lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;  
&lt;tx:attributes&gt;  
&lt;tx:method name="insert\*" propagation="REQUIRED"/&gt;  
&lt;tx:method name="update\*" propagation="REQUIRED"/&gt;  
&lt;tx:method name="delete\*" propagation="REQUIRED"/&gt;  
&lt;tx:method name="upload\*" propagation="REQUIRED"/&gt;  
&lt;tx:method name="\*" propagation="SUPPORTS" read-only="true"/&gt;  
&lt;/tx:attributes&gt;  
&lt;/tx:advice&gt;

&lt;/beans&gt;

UserDao应该是个接口类，需要实现该类并配置Bean

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {  
public void updateInfo() throws DataAccessException {

}  
}

&lt;bean id="UserDao" class="cn.idtag.dao.UserDaoImpl"&gt;  
&lt;property name="dataSource" ref="dataSource"/&gt;  
&lt;/bean&gt;

一、

&lt;bean id="transactionManager"  
class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref local="sessionFactory" /&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="catDao" class="cn.terry.spring.hibernate.CatDao"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref local="sessionFactory" /&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="catDaoProxy"  
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;  
&lt;property name="transactionManager"&gt;  
&lt;ref bean="transactionManager" /&gt;  
&lt;/property&gt;  
&lt;property name="target"&gt;  
&lt;ref local="catDao" /&gt;  
&lt;/property&gt;  
&lt;property name="transactionAttributes"&gt;  
&lt;props&gt;  
&lt;prop key="insert\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

二、&lt;bean id="transactionManager"  
class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory" ref="sessionFactory" /&gt;  
&lt;/bean&gt;

&lt;bean id="baseTxService"  
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
abstract="true"&gt;  
&lt;property name="transactionManager" ref="transactionManager" /&gt;  
&lt;property name="proxyTargetClass" value="true" /&gt;

&lt;property name="transactionAttributes"&gt;  
&lt;props&gt;  
&lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
&lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
&lt;prop key="load\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
&lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;prop key="remove\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

 

&lt;beans default-autowire="byName"&gt;  
&lt;bean id="userService" parent="baseTxService"&gt;  
&lt;property name="target"&gt;  
&lt;bean class="com.pms.service.UserService"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;

三、

&lt;beans

&lt;!--spring数据源配置--&gt;  
&lt;bean id="DataSource"  
class="org.apache.commons.dbcp.BasicDataSource"&gt;  
&lt;property name="driverClassName"&gt;  
&lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
&lt;/property&gt;  
&lt;property name="url"&gt;  
&lt;value&gt;jdbc:mysql://localhost:3306/test&lt;/value&gt;  
&lt;/property&gt;  
&lt;property name="username"&gt;  
&lt;value&gt;root&lt;/value&gt;  
&lt;/property&gt;  
&lt;property name="password"&gt;  
&lt;value&gt;mysql&lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!--sessionFactory 配置hibernate 映射--&gt;  
&lt;bean id="sessionFactory"  
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
&lt;property name="dataSource"&gt;  
&lt;ref bean="DataSource" /&gt;  
&lt;/property&gt;  
&lt;property name="hibernateProperties"&gt;  
&lt;props&gt;  
&lt;prop key="hibernate.dialect"&gt;  
org.hibernate.dialect.MySQLDialect  
&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;property name="mappingResources"&gt;  
&lt;list&gt;  
&lt;value&gt;com/hibernate/ormXml/Customers.hbm.xml&lt;/value&gt;  
&lt;value&gt;  
com/hibernate/ormXml/Customeraddress.hbm.xml  
&lt;/value&gt;  
&lt;/list&gt;  
&lt;/property&gt;

&lt;!--DAO层引用sessionFactory--&gt;  
&lt;/bean&gt;  
&lt;bean id="CustomersDAO"  
class="com.hibernate.dao.CustomersDAO"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory" /&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="CustomeraddressDAO"  
class="com.hibernate.dao.CustomeraddressDAO"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory" /&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

 

&lt;!--事务配置开始--&gt;

&lt;!--hibernate transaction \[start]--&gt;

&lt;!-- declare transaction hibernate \[start] (事务分二种，以下为声明式事务) --&gt;  
&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref local="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="exampler1\_service" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;  
&lt;property name="transactionManager"&gt;  
&lt;ref local="transactionManager"/&gt;  
&lt;/property&gt;  
&lt;property name="target"&gt;  
&lt;ref local="CustomersDAO"/&gt;  
&lt;/property&gt;  
&lt;property name="transactionAttributes"&gt;  
&lt;props&gt;  
&lt;prop key="get\*"&gt;  
PROPAGATION\_REQUIRED,readOnly  
&lt;/prop&gt;  
&lt;prop key="set\*"&gt;  
PROPAGATION\_REQUIRED,readOnly  
&lt;/prop&gt;  
&lt;prop key="save"&gt;  
PROPAGATION\_REQUIRED,readOnly  
&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;!--hibernate transaction \[end]--&gt;  
&lt;!-- declare transaction hibernate \[end] --&gt;

&lt;!-- programing transaction hibernate (编程式事务) \[start] --&gt;  
&lt;bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"&gt;  
&lt;property name="transactionManager"&gt;  
&lt;ref local="transactionManager"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref local="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;!-- programing transaction hibernate \[end] --&gt;

&lt;!--其它事务配置--&gt;

&lt;!-- JDBC TransactionManager \[start]--&gt;  
&lt;bean id="transactionManager\_jdbc" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
&lt;property name="dataSource"&gt;  
&lt;ref local="dataSource\_jdbc"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="dataSource\_jdbc" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;  
&lt;property name="jndiName"&gt;  
&lt;value&gt;java:/MySqlDs&lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;!--JDBC TransactionManager \[end] --&gt;

&lt;!-- JDO TransactionManager \[start]--&gt;  
&lt;bean id="transactionManager\_jdo" class="org.springframework.orm.jdo.JdoTransactionManager"&gt;  
&lt;property name="persistenceManagerFactory"&gt;  
&lt;ref local="dataSource\_jdbc"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;!-- JDO TransactionManager \[end] --&gt;

&lt;!-- JTA TransactionManager \[start]--&gt;  
&lt;bean id="transactionManager\_jta" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;  
&lt;property name="userTransactionName"&gt;  
&lt;value&gt;java:comp/UserTransaction&lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;!-- JTA TransactionManager \[end] --&gt;

&lt;!--事务配置结束--&gt;