`

spring事物(本文为转载,地址:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)

阅读更多

Spring事务配置的五种方式

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

    总结如下:

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

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

    具体如下图:

Spring事务配置 (2)

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

    第一种方式:每个Bean都有一个代理

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?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共享一个代理基类

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?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>

第三种方式:使用拦截器

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?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标签配置的拦截器

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?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>

第五种方式:全注解
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><?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注解,如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->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();
    }
   
   
}
分享到:
评论

相关推荐

    http://www.blogjava.net/youlq/archive/2005/12/06/22678.html

    标题中的链接指向了一篇关于Acegi安全框架在Spring框架中使用的文章,而描述中的博文链接则是另一篇关于技术讨论的文章。结合标签“源码”和“工具”,我们可以推测这篇文章主要探讨的是如何通过Acegi安全框架来增强...

    集XMPP推送与聊天于一体的Openfire开发Android

    与标题一致的描叙,你值得拥有,确实很值。来赚分啦。 参考下面大侠而来 http://blog.csdn.net/xutaozero21/article/details/4873439 ...http://www.blogjava.net/lizongbo/archive/2006/12/04/85433.html

    java常用网址,可能已经过期了

    - [http://www.blogjava.net/wangxinsh55/archive/2006/04/27/43535.html](http://www.blogjava.net/wangxinsh55/archive/2006/04/27/43535.html) - **知识点**:Git是一款非常流行的分布式版本控制系统,广泛应用...

    hadoop学习资料地址

    4. **Killme2008的博客**:`http://www.blogjava.net/killme2008/archive/2008/06/05/206043.html` - 深入解析了Hadoop的架构设计和关键点。 5. **CSDN博客** - `...

    flash动态图片的自动播放效果

    0x000000&bcastr_flie=1.jpg|2.jpg|3.jpg|4.jpg|5.jpg&bcastr_link=http://www.baidu.com|http://www.google.com|http://www.sina.com.cn|http://bbs.crsky.com|http://www.blogjava.net/supercrsky&bcastr_title=...

    compass包及相关学习资料

    Compass是一个强大的,事务的,高性能的对象/搜索引擎映射(OSEM:object/search engine mapping)与一个Java持久层框架.内容包括compass的jar包及从网上搜集的相关学习资料. 附两个不错的资料地址: ...

    文件上传的解决方案总结暨CSV文件解析

    第二种是用Apache组织的commons项目中的FileUpload组件,参考资料http://www.blogjava.net/lushengdi/archive/2009/01/09/227063.html。 今天介绍第二种方案中的文件上传,文件下载功能则可细想而知。 第二种方案中...

    android学习资料(网站,论坛)

    3. **TiGERTiAN博客(http://www.blogjava.net/TiGERTiAN/archive/2010/02/03/311827.html)**:专注于Java和Android技术的深度解析。 4. **CSDN博客...

    百度mp3Searcher

    Swing 程序 软件声明 ========== 本软件为免费开源软件!仅供学习研究使用,未供许可请不要作用于任何商业目的!欢迎学习交流!...Blog: http://www.blogjava.net/huliqing/ home: http://www.tbuy.biz/

    Eclipse 设置、调优、使用(解决 卡顿 编译 占内存等问题)

    1. JVM 启动参数大全:http://www.blogjava.net/midstr/archive/2008/09/21/230265.html 2. JVM 结构的一些知识:http://hllvm.group.iteye.com/group/wiki/2905-JVM 3. JVM 堆知识:...

    jadclipse反编译工具包

    包括jadclipse的jar包以及jad.exe.使用步骤详见以下网址: eclipse: http://www.blogjava.net/landon/archive/2010/07/16/326294.html myEclipse: http://hunter090730.iteye.com/blog/1578425

    python使用PIL库读取图片的像素值

    Python 使用 PIL 库读取图片的像素值 本文主要介绍了如何使用 Python 的 PIL 库读取图片的像素值,以便在游戏开发中删除不需要的...* PIL 库教程:&lt;http://www.blogjava.net/Skynet/archive/2009/11/28/303986.html&gt;

    webservices示例工程

    1.webservieces 示例工程源码 里面有两个...http://www.blogjava.net/RongHao/archive/2007/06/12/123638.html(axis1) http://wenku.baidu.com/view/1d9b3d2ecfc789eb172dc8a5.html(axis2) 5.欢迎大家踊跃下载分享传播

    JavaDOCHelper1.5.1_Viewer

    JavaDOCHelper Viewer 提供了在一个界面里查看、搜索众多组件的HTML格式JAVA DOC api文档,类似于chm格式。 软件界面:http://www.blogjava.net/Unmi/archive/2007/10/08/150946.html

    Struts2_Validation

    //wiki.javascud.org/display/ww2cndoc/Valida&lt;br&gt;tion 网站上收集的一些关于有关Struts2验证框架的资料,并将其做成电子书以方便查看,另外向朋友们推荐一个很好的学习Struts2的博客http://www.blogjava.net/max,...

    MFC简易文章编辑器(Brute Froce算法)(源码+DOC+PPT)

    http://iunbug.appspot.com/ 网络阅读可访问:http://www.blogjava.net/tidelgl 网络阅读效果不是很好.推荐下载.

    MyEclipse 6 Java 开发中文教程2011

    亲爱的读者, 当您掌握了本书的内容后, 应立即掌握以Google来阅读文章提高自己能力的自学之路, 并加入到企业投入到真正企业项目的锻炼中去, ...链接:http://www.blogjava.net/beansoft/archive/2010/09/09/331571.html

    解决log4j:ERROR Failed to rename代码包

    log4j:ERROR Failed to rename错误解决办法 http://www.blogjava.net/DreamAngel/archive/2011/11/10/363400.html

Global site tag (gtag.js) - Google Analytics