前段时间对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. hibernate操作步骤(代码部分)

    1.加载hibernate的核心配置文件 2.创建SessionFactory对象 3.使用SessionFactory创建Session对象 4.开启事务(手动开启) 5.写具体逻辑crud,增删改 ...

  2. # ? & 号在url中的的作用

    1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http://twitter.com/username改版后,就变 ...

  3. ionic 项目中添加modal的步骤流程

    1.首先在templates文件夹下面定义一个新页面,xxx.html,template文件夹在空项目里面是没有的,需要手动添加一个,放在WWW文件夹下面. <ion-modal-view> ...

  4. 关于EasyUI中的Tree

    2017年6月21日,天气阴.心情比较沉重. 近期由于毕设的事情,三周不写代码了.这周测试提交了一些BUG,于是开始着手处理,还真的是熟能生巧,三周的功夫就感觉有点生疏.其中有一个BUG就是角色对应的 ...

  5. JavaWeb 环境搭建

    环境搭建 JDK7 Java基本开发工具包 安装(目录[不要使用中文和空格].JDK+JRE) 配置环境变量[JAVA_HOME.path.classpath] 2.        Tomcat7 提 ...

  6. [USACO4.2]草地排水Drainage Ditches

    题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草 要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹 ...

  7. 解决Socket粘包问题——C#代码

    解决Socket粘包问题——C#代码 前天晚上,曾经的一个同事问我socket发送消息如果太频繁接收方就会有消息重叠,因为当时在外面,没有多加思考 第一反应还以为是多线程导致的数据不同步导致的,让他加 ...

  8. Java总结之线程(1)

    java线程是很重要的一项,所以作为java程序员必须要掌握的. 理解java线程必须先理解线程在java中的生命周期.. 1.java线程生命周期 1.new  创建一个线程  java中创建线程有 ...

  9. 实现自己的.NET Core配置Provider之Yaml

    YAML是一种更适合人阅读的文件格式,很多大型的项目像Ruby on Rails都选择YAML作为配置文件的格式.如果项目的配置很少,用JSON或YAML没有多大差别.看看rails项目中的配置文件, ...

  10. Spring Boot 自动重启(spring-boot-devtools)

    原文 https://github.com/x113773/testall/issues/8 1. 首先添加依赖```<dependency><groupId>org.spri ...