---
title: "spring1.x spring2的声明式事务配置"
author: "e3002"
date: "2008-04-09T09:51:00Z"
tags: []
slug: "spring1-x-spring2"
draft: false
lastmod: "2008-04-09T09:51:00Z"
id: 231979
url: "https://e3002.iteye.com/blog/231979"
---

spring1的声明式事务配置：

    &lt;!-- 事务的处理 --&gt;  
    &lt;!-- Hibernate使用的事物管理器--&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="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
     &lt;property name="dataSource" ref="jdbcDataSource"&gt;&lt;/property&gt;  
    &lt;/bean&gt;

    &lt;!-- 事务处理代理的抽象类,可被继承使用--&gt;  
    &lt;bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"&gt;  
        &lt;property name="transactionManager"&gt;  
            &lt;ref bean="transactionManager" /&gt;  
        &lt;/property&gt;  
        &lt;property name="transactionAttributes"&gt;  
            &lt;props&gt;  
                &lt;prop key="create\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
                &lt;prop key="remove\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
                &lt;prop key="confirm\*"&gt;PROPAGATION\_REQUIRED, -BetException&lt;/prop&gt;  
                &lt;prop key="update\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
                &lt;prop key="set\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
                &lt;prop key="do\*"&gt;PROPAGATION\_REQUIRED, -SettleException&lt;/prop&gt;  
                &lt;prop key="settle"&gt;PROPAGATION\_REQUIRED, -Exception&lt;/prop&gt;  
                &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
            &lt;/props&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="jdbcTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"&gt;  
     &lt;property name="transactionManager" ref="jdbcTransactionManager"&gt;&lt;/property&gt;  
     &lt;property name="transactionAttributes"&gt;  
      &lt;props&gt;  
       &lt;prop key="refuseGaming"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
      &lt;/props&gt;  
     &lt;/property&gt;  
    &lt;/bean&gt;

 

因为我们的事务一般都放在service层，ok 看下service是如何与上面的事务配置联系起来的

    &lt;bean id="matchService" parent="baseTransactionProxy"&gt;  
        &lt;property name="target"&gt;  
            &lt;bean class="com.airinbox.gamester.service.impl.MatchService"&gt;  
                &lt;property name="matchDao"&gt;  
                    &lt;ref bean="matchHibernateDao" /&gt;  
                &lt;/property&gt;  
                &lt;property name="gamblingDao"&gt;  
                    &lt;ref bean="gamblingHibernateDao" /&gt;  
                &lt;/property&gt;  
                &lt;property name="teamDao"&gt;  
                    &lt;ref bean="teamHibernateDao" /&gt;  
                &lt;/property&gt;  
                &lt;property name="starDao"&gt;  
                    &lt;ref bean="starHibernateDao" /&gt;  
                &lt;/property&gt;  
            &lt;/bean&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;

ok，spring1.x事务配置完成，下面看spring2的配置简单了很多

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

 &lt;!-- 以AspectJ方式 定义 AOP --&gt;  
 &lt;aop:config &gt;  
  &lt;!-- 注意,请把第2个\*号换为项目package 这里的写法可以参考spring手册 --&gt;  
  &lt;aop:advisor pointcut="execution(* \*..service.\*Manager.\*(..))" advice-ref="txAdvice"/&gt;  
  &lt;aop:advisor pointcut="execution(* org.springside.core.dao.\*Dao.\*(..))" advice-ref="txAdvice"/&gt;  
 &lt;/aop:config&gt;

 &lt;!-- 基本事务定义,使用transactionManager作事务管理,默认get\*方法的事务为readonly,其余方法按默认设置.  
    默认的设置请参考Spring文档事务一章. --&gt;  
 &lt;tx:advice id="txAdvice"&gt;  
  &lt;tx:attributes&gt;  
   &lt;tx:method name="get\*" read-only="true"/&gt;  
   &lt;tx:method name="find\*" read-only="true"/&gt;  
   &lt;tx:method name="\*"/&gt;  
  &lt;/tx:attributes&gt;  
 &lt;/tx:advice&gt;

是不是简单了很多！