---
title: "Spring配置事务"
author: "hlbng"
date: "2009-06-10T14:04:27Z"
tags: []
slug: "spring"
draft: false
lastmod: "2009-06-10T14:04:27Z"
id: 405853
url: "https://hlbng.iteye.com/blog/405853"
---

在Spring中，通过实现org.springframework.transaction.PlatformTransactionManager接口能达到多种持久化框架的事务管理。

 

持久化方案

Spring中配置事务的相对应类

JDBC

org.springframework.jdbc.datasource.DataSourceTransactionManager

JTA

org.springframework.transaction.jta.JtaTransactionManager

Hibernate

org.springframework.orm.hibernate3.HibernateTransactionManager

JPA

org.springframework.orm.jpa.JpaTransactionManager

 

Spring提供的事务管理器仅仅是对现有的事务实现API（Hibernate、JDBC、JTA）进行封装，其本身并没有提供具体的事务服务实现。

 

在Spring中事务在org.springframework.transaction.TransactionDefinition接口中定义。如果想深入了解Sping中的事务机制，必须要了解这个接口。

 

假如我要达到如下要求：在特定的类中，如果方法名称是以insert，delete开头的方法要支持当前事务安全，如果当前没有事务，就新建一个事务，其他的方法只读。

 

下面就以上要求介绍两种在Spring中常用的事务配置方法：

 

方法一：用BeanNameAutoProxyCreator自动创建事务代理

```
<!--   -->   
<bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
       <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<!-- 定义事务规则的拦截器 -->   
<bean id="transactionInterceptor"   
    class="org.springframework.transaction.interceptor.TransactionInterceptor">   
    <property name="transactionManager" ref="transactionManager" />   
    <property name="transactionAttributes">   
       <props>   
           <prop key="insert*">PROPAGATION_REQUIRED</prop>   
           <prop key="delete*">PROPAGATION_REQUIRED </prop>   
           <prop key="*">readOnly</prop>   
       </props>   
    </property>   
</bean>   
<!-- 添加要实现事务规则的BeanName -->   
<bean   
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
    <property name="beanNames">   
       <list>   
           <value>persondao</value>   
       </list>   
    </property>   
    <property name="interceptorNames">   
       <list>   
           <value>transactionInterceptor</value>   
       </list>   
    </property>   
</bean>   
   
<bean id="hibernateTemplate"      class="org.springframework.orm.hibernate3.HibernateTemplate">   
    <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<bean id="persondao"   
       class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">   
    <property name="hibernateTemplate" ref="hibernateTemplate" />   
</bean>   
```

 

方法二：利用AOP原理来管理事务

 

随着Sping项目代码量变大，你会发现配置越来越来多，且繁琐。在Sping2.0之后，Sping推出了简化的XML配置。具体可以看江南白衣的《[简化Spring--配置文件](http://www.blogjava.net/calvin/archive/2005/08/21/10530.html)》。

 

在使用方法二之前，需要加入包如下包:aspectjrt.jar、aspectjweaver.jar

 

且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:p="http://www.springframework.org/schema/p"   
    xmlns:aop="http://www.springframework.org/schema/aop"   xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:jee="http://www.springframework.org/schema/jee"   
    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.5.xsd    
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd    
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd    
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">   
```

 接着配置事务

```
<bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
    <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<!-- 这里的配置有AOP的思想，找的这个面就是com.sms.freeborders.dao.hbimpl下的所有类-->   
   
<aop:config>   
    <aop:pointcut id="allManagerMethod"   
       expression="execution(* com.sms.freeborders.dao.hbimpl.*.*(..))" />   
    <aop:advisor advice-ref="txAdvice"   
       pointcut-ref="allManagerMethod" />   
</aop:config>   
   
<!-- 配置事务规则 -->   
   
<tx:advice id="txAdvice">   
    <tx:attributes>   
       <tx:method name="insert *" />   
       <tx:method name="delete*" />   
       <tx:method name="*" read-only="true" />   
    </tx:attributes>   
</tx:advice>   
   
<bean id="hibernateTemplate"   class="org.springframework.orm.hibernate3.HibernateTemplate">   
       <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<bean id="persondao"   
       class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">   
    <property name="hibernateTemplate" ref="hibernateTemplate" />   
</bean>   
```