`

spring_事物处理配置

 
阅读更多

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

    DataSourceTransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactoryTransactionManager的实现为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();
    }
    
    

  • 大小: 30 KB
分享到:
评论

相关推荐

    Spring_事物的写法

    根据提供的文件信息,本文将详细解析Spring...通过上述配置,我们可以看到Spring如何通过声明式事务管理、AOP和配置文件的结合,为应用提供了一种简单且高效的事务处理方案。这对于提高代码质量和维护性具有重要意义。

    spring中事物配置

    然后在其他地方,如DAO层,你需要注入接口而不是实现类,Spring会自动创建代理对象并处理事务。 总结一下,Spring的声明式事务管理通过AOP机制,使得开发者无需在业务代码中显式地开始和结束事务,而是通过配置决定...

    spring的5中事物配置 介绍spring的5中事物配置

    Spring 框架提供了多种事务配置方式,这些配置方法主要基于Spring的AOP(面向切面编程)来实现事务管理。下面将详细介绍Spring中的五种事务配置方式。 1. **基于代理的事务管理(Proxy-based Transaction Management...

    Spring声明式事务配置模板2.x

    在Spring框架中,声明式事务管理是其核心特性之一,它极大地简化了应用程序中的事务处理。Spring 2.x版本的声明式事务配置模板是开发者常用的一种方式,它通过AOP(面向切面编程)实现事务的自动管理,使得开发者...

    spring事物和rabbitMQ的例子

    在"spring的2个数据源的配置"中,这意味着系统可能需要处理来自两个不同数据源的数据,例如一个用于主业务数据,另一个用于日志或审计记录。Spring允许我们为每个数据源配置单独的事务管理器,这样可以独立控制各自...

    第十四章 Spring4 切面事物与事物通知与传播行为

    本章将详细探讨Spring4中的切面事务处理、事物通知以及传播行为。 一、切面编程(AOP) 切面编程是Spring框架的一大特色,它允许开发者将关注点如日志记录、事务管理等从主业务逻辑中分离出来,形成独立的模块,即...

    Spring源代码解析(六):Spring声明式事务处理.doc

    在 Spring 中,声明式事务处理可以通过配置 TransactionProxyFactoryBean 来实现。TransactionProxyFactoryBean 是一个 FactoryBean,它可以创建一个代理对象,该代理对象可以拦截业务方法的调用,并对事务进行管理...

    最新版本SpringCloud+seata+nacos实现全局事物处理的demo案例

    版本:springcloud 2021.0.1,springboot 2.6.4,seata 1.4.2,nacos 2.0.3 (其他所有依赖版本均为最新mybatis-plus,fegin等等......) 模拟订单和库存的一个简单案例实现,注册到nacos配置中心,通过feign远程调用...

    spring-tx事物源码

    在这个源码中,我们可以深入理解Spring如何处理事务的生命周期、回滚规则以及与各种数据源的集成。 首先,`spring-tx`模块主要包含以下几个关键组件: 1. **PlatformTransactionManager**: 这是Spring事务管理的...

    SSI框架搭建实例教程(struts spring ibatis整合 附切面事物处理)

    【SSI框架搭建实例教程(struts spring ibatis整合 附切面事物处理)】 在软件开发中,集成多种框架可以提高应用程序的灵活性和可维护性。SSI框架是指Struts、Spring和iBatis的集成,这三种框架分别负责MVC模式中的...

    spring 事务(6中配置完全降解)

    本篇文章将详细解析Spring中的六种事务配置方法,帮助开发者深入理解并掌握Spring事务的运用。 1. **基于XML的事务配置** Spring支持通过XML配置来管理事务,这是最基础的配置方式。在`spring`的配置文件中,我们...

    在SSH中使用事物包括SSH的搭建和配置;事物的配置;注释详细

    在配置文件中,注释应详细说明每个元素的作用,例如在Spring的配置文件中,`&lt;tx:annotation-driven&gt;`的`transaction-manager`属性指定哪个事务管理器处理事务,`proxy-target-class="true"`表示使用CGLIB代理而不是...

    spring中事物管理1

    在 Spring 框架中,事务管理是确保应用程序在处理数据库操作时遵循 ACID(原子性、一致性、隔离性和持久性)原则的关键组件。ACID 是事务处理的基本准则,保证了数据的一致性和完整性。 1. 原子性(Atomicity):...

    事物简单总结(偏向Spring事物)

    本文主要关注Spring框架中的事务管理,它提供了一种声明式和编程式的事务处理方式,使得开发者能够方便地在应用层面上控制事务。 首先,我们需要理解事务的基本概念。事务是数据库操作的基本单元,它包含一组操作,...

    spring-控制事物

    在描述中提到了一个博客链接,虽然具体内容无法直接获取,但通常博主会分享关于Spring事务管理的实践案例、配置方式或原理分析。这种博客通常会涵盖事务的隔离级别(如读未提交、读已提交、可重复读、串行化)、事务...

    spring编程式事物

    标题"spring编程式事物"主要关注的是编程式事务管理。这种方式允许开发者通过`PlatformTransactionManager`接口和`TransactionDefinition`接口来控制事务的生命周期。在Spring中,通常使用`TransactionTemplate`或者...

    Spring事务管理Demo

    在Spring框架中,事务管理是核心特性之一,它允许开发者以声明式或编程式的方式处理应用中的事务。Spring事务管理的目的是确保数据的一致性和完整性,尤其是在多操作、多资源的环境中。本Demo将深入探讨Spring如何...

    spring事物的五种配制方法

    ### Spring事务的五种配置方法详解 #### 一、引言 在软件开发过程中,事务管理是确保数据一致性的重要手段之一。Spring框架提供了强大的事务管理功能,能够方便地与多种数据库交互,支持不同的数据访问技术如JDBC、...

    spring-控制事物回滚

    本文将深入探讨“spring-控制事物回滚”这一主题,结合标签“源码”和“工具”,我们将从源代码层面理解Spring如何控制事务的回滚,并介绍一些实用的工具和技巧。 首先,我们需要了解什么是事务。事务是一组数据库...

Global site tag (gtag.js) - Google Analytics