`

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

    NULL 博文链接:https://lengjing.iteye.com/blog/668310

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

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

    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=...

    Tomcat服务器的安装、配置及修改目录.doc

    HYPERLINK"http://www.blogjava.net/beansoft/archive/2007/07/19/131286.html"Tomcat服务器配置、运行、更改目录的整理一、配置Tomcat服务器1)下载并安装对应操作系统上的JDK5或者6HYPERLINK...

    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。 今天介绍第二种方案中的文件上传,文件下载功能则可细想而知。 第二种方案中...

    百度mp3Searcher

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

    jadclipse反编译工具包

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

    webservices示例工程

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

    解决log4j:ERROR Failed to rename代码包

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

    oracle分析函数学习

    1)Oracle开发专题99%收集自: http://www.blogjava.net/pengpenglin/(偶补充了一点点1%); 2) PLSQL开发笔记和小结收集自http://www.blogjava.net/cheneyfree/ 3)分析函数简述收集自http://space.itpub.net/7607759/ ...

    对google个性主页的拖拽效果的js的完整注释[转]

    作者:Tin出处:http://www.blogjava.net/iamtin/archive/2006/04/27/43668.html代码:http://www.blogjava.net/Files/iamtin/google_drag.rar 代码如下:// 工具类,使用Util的命名空间,方便管理 var Util = ...

    JavaDOCHelper1.5.1_Viewer

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

    Visual C++ 6.0调试功能 图解教程(3)--实例二

    http://iunbug.appspot.com/ 网络阅读可访问:http://www.blogjava.net/tidelgl

    Visual C++ 6.0调试功能 图解教程(2)--实例一

    http://iunbug.appspot.com/ 网络阅读可访问:http://www.blogjava.net/tidelgl

    Visual C++ 6.0调试功能 图解教程(4)--实例三

    http://iunbug.appspot.com/ 网络阅读可访问:http://www.blogjava.net/tidelgl

    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

    Struts1.x系列教程 pdf(清晰)

    文章来自:http://www.blogjava.net/nokiaguy/

Global site tag (gtag.js) - Google Analytics