前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

总结如下:

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

具体如下图:

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

第一种方式:每个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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean
id="sessionFactory" 

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

        <property
name="configLocation" value="classpath:hibernate.cfg.xml" /> 

        <property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

<!-- 定义事务管理器(声明式的事务)
--> 
    <bean
id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <!-- 配置DAO
-->
    <bean
id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <bean
id="userDao" 

        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 

           <!-- 配置事务管理器
--> 
           <property
name="transactionManager" ref="transactionManager" />    

        <property
name="target" ref="userDaoTarget" /> 

         <property
name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
        <!-- 配置事务属性
--> 
        <property
name="transactionAttributes"> 

            <props> 

                <prop
key="*">PROPAGATION_REQUIRED</prop>
            </props> 

        </property> 

    </bean> 

</beans>

第二种方式:所有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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean
id="sessionFactory" 

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

        <property
name="configLocation" value="classpath:hibernate.cfg.xml" /> 

        <property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

<!-- 定义事务管理器(声明式的事务)
--> 
    <bean
id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <bean
id="transactionBase" 

            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 

            lazy-init="true" abstract="true"> 

        <!-- 配置事务管理器
--> 
        <property
name="transactionManager" ref="transactionManager" /> 

        <!-- 配置事务属性
--> 
        <property
name="transactionAttributes"> 

            <props> 

                <prop
key="*">PROPAGATION_REQUIRED</prop> 

            </props> 

        </property> 

    </bean>   

  
    <!-- 配置DAO
-->
    <bean
id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <bean
id="userDao" parent="transactionBase" > 

        <property
name="target" ref="userDaoTarget" />  

    </bean>
</beans>

第三种方式:使用拦截器

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean
id="sessionFactory" 

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

        <property
name="configLocation" value="classpath:hibernate.cfg.xml" /> 

        <property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

<!-- 定义事务管理器(声明式的事务)
--> 
    <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="*">PROPAGATION_REQUIRED</prop> 

            </props> 

        </property> 

    </bean>
     
    <bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 

        <property
name="beanNames"> 

            <list> 

                <value>*Dao</value>
            </list> 

        </property> 

        <property
name="interceptorNames"> 

            <list> 

                <value>transactionInterceptor</value> 

            </list> 

        </property> 

    </bean> 

 
    <!-- 配置DAO
-->
    <bean
id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

第四种方式:使用tx标签配置的拦截器

<?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"
    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.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config
/>
    <context:component-scan
base-package="com.bluesky" />

<bean
id="sessionFactory" 

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

        <property
name="configLocation" value="classpath:hibernate.cfg.xml" /> 

        <property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

<!-- 定义事务管理器(声明式的事务)
--> 
    <bean
id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>

<tx:advice
id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method
name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
   
    <aop:config>
        <aop:pointcut
id="interceptorPointCuts"
            expression="execution(* com.bluesky.spring.dao.*.*(..))" />
        <aop:advisor
advice-ref="txAdvice"
            pointcut-ref="interceptorPointCuts" />       

    </aop:config>     

</beans>

第五种方式:全注解

<?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"
    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.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config
/>
    <context:component-scan
base-package="com.bluesky" />

<tx:annotation-driven
transaction-manager="transactionManager"/>

<bean
id="sessionFactory" 

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

        <property
name="configLocation" value="classpath:hibernate.cfg.xml" /> 

        <property
name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>

<!-- 定义事务管理器(声明式的事务)
--> 
    <bean
id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property
name="sessionFactory" ref="sessionFactory" />
    </bean>
   
</beans>

此时在DAO上需加上@Transactional注解,如下:

package com.bluesky.spring.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.bluesky.spring.domain.User;

@Transactional
@Component("userDao")
public class UserDaoImpl
extends HibernateDaoSupport
implements UserDao {

public List<User> listUsers() {
        return this.getSession().createQuery("from
User").list();
    }
   
   
}

(转)Spring事务配置的五种方式的更多相关文章

  1. Spring事务配置的五种方式(转发)

    Spring事务配置的五种方式(原博客地址是http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)挺好的,收藏转发 前段时间对Sp ...

  2. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

    转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...

  3. Spring事务配置的五种方式(转载)

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  4. Spring事务配置的五种方式

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  5. Spring事务配置的五种方式 -- 越往后需要Spring版本越高

    第五种 基本零配置  个人感觉第四种也可以 Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式, ...

  6. Spring事务配置的五种方式(转)

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  7. Spring事务配置的五种方式 巨全!不看后悔,一看必懂!

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  8. SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式

    这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...

  9. [JavaEE] Spring事务配置的五种方式

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

随机推荐

  1. 关于getHTML()方法和getHtmlAjax()方法 GetHttpLength, 清除HTML标签

    public string getHtml(string Url, string type = "UTF-8") { try { System.Net.WebRequest wRe ...

  2. scrapy抓取淘宝女郎

    scrapy抓取淘宝女郎 准备工作 首先在淘宝女郎的首页这里查看,当然想要爬取更多的话,当然这里要查看翻页的url,不过这操蛋的地方就是这里的翻页是使用javascript加载的,这个就有点尴尬了,找 ...

  3. python 标准库 -- re

    re 正则表达式 语法 import re m = re.search('[0-9]','abc4def67') # 匹配字符及匹配范围 print m.group(0) # 返回匹配结果 re.se ...

  4. R语言统计分析技术研究——岭回归技术的原理和应用

    岭回归技术的原理和应用 作者马文敏 岭回归分析是一种专用于共线性分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息,降低精度为代价获得回归系数更为符合 ...

  5. Java基础知识整理(一)

    Java开发环境JDK(Java编辑器.Java运行工具(JRE作用).Java文档生成工具.Java打包工具) 1.Java是严格区分大小写的.2.Java程序中一句连续的字符串不能分开在两行书写, ...

  6. Java注解--实现动态数据源切换

    当一个项目中有多个数据源(也可以是主从库)的时候,我们可以利用注解在mapper接口上标注数据源,从而来实现多个数据源在运行时的动态切换. 实现原理 在Spring 2.0.1中引入了Abstract ...

  7. AngularJS事件

    <body ng-app="myApp"> <div ng-controller="myCtrl"> <button ng-cli ...

  8. 12.exception对象

    excepton对象是一个异常对象,当一个页面在运行过程中发生了异常,就产生了这个对象,如果一个jsp页面要应用此对象,就必须把isErrorPage设置为true,否则无法编译.它实际上是java. ...

  9. python实现折半查找算法&&归并排序算法

    今天依旧是学算法,前几天在搞bbs项目,界面也很丑,评论功能好像也有BUG.现在不搞了,得学下算法和数据结构,笔试过不了,连面试的机会都没有…… 今天学了折半查找算法,折半查找是蛮简单的,但是归并排序 ...

  10. C# 模拟跑马灯效果(2种)

    #region 跑马灯效果方法 /// <summary> /// 文字进入左侧后从右侧出来 /// </summary> private void LabelRun() { ...