`
hlbng
  • 浏览: 176638 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring配置事务

阅读更多

在Spring中,通过实现org.springframework.transaction.PlatformTransactionManager接口能达到多种持久化框架的事务管理。

 

持久化方案

Spring中配置事务的相对应类

JDBC

org.springframework.jdbc.datasource.DataSourceTransactionManager

JTA

org.springframework.transaction.jta.JtaTransactionManager

Hibernate

org.springframework.orm.hibernate3.HibernateTransactionManager

JPA

org.springframework.orm.jpa.JpaTransactionManager

 

Spring提供的事务管理器仅仅是对现有的事务实现API(Hibernate、JDBC、JTA)进行封装,其本身并没有提供具体的事务服务实现。

 

在Spring中事务在org.springframework.transaction.TransactionDefinition接口中定义。如果想深入了解Sping中的事务机制,必须要了解这个接口。

 

假如我要达到如下要求:在特定的类中,如果方法名称是以insert,delete开头的方法要支持当前事务安全,如果当前没有事务,就新建一个事务,其他的方法只读。

 

下面就以上要求介绍两种在Spring中常用的事务配置方法:

 

方法一:用BeanNameAutoProxyCreator自动创建事务代理

<!--   -->   
<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="insert*">PROPAGATION_REQUIRED</prop>   
           <prop key="delete*">PROPAGATION_REQUIRED </prop>   
           <prop key="*">readOnly</prop>   
       </props>   
    </property>   
</bean>   
<!-- 添加要实现事务规则的BeanName -->   
<bean   
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
    <property name="beanNames">   
       <list>   
           <value>persondao</value>   
       </list>   
    </property>   
    <property name="interceptorNames">   
       <list>   
           <value>transactionInterceptor</value>   
       </list>   
    </property>   
</bean>   
   
<bean id="hibernateTemplate"      class="org.springframework.orm.hibernate3.HibernateTemplate">   
    <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<bean id="persondao"   
       class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">   
    <property name="hibernateTemplate" ref="hibernateTemplate" />   
</bean>   

 

方法二:利用AOP原理来管理事务

 

随着Sping项目代码量变大,你会发现配置越来越来多,且繁琐。在Sping2.0之后,Sping推出了简化的XML配置。具体可以看江南白衣的《简化Spring--配置文件》。

 

在使用方法二之前,需要加入包如下包:aspectjrt.jar、aspectjweaver.jar

 

且applicationContext.xml的头部定义如下:

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

 接着配置事务

<bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
    <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<!-- 这里的配置有AOP的思想,找的这个面就是com.sms.freeborders.dao.hbimpl下的所有类-->   
   
<aop:config>   
    <aop:pointcut id="allManagerMethod"   
       expression="execution(* com.sms.freeborders.dao.hbimpl.*.*(..))" />   
    <aop:advisor advice-ref="txAdvice"   
       pointcut-ref="allManagerMethod" />   
</aop:config>   
   
<!-- 配置事务规则 -->   
   
<tx:advice id="txAdvice">   
    <tx:attributes>   
       <tx:method name="insert *" />   
       <tx:method name="delete*" />   
       <tx:method name="*" read-only="true" />   
    </tx:attributes>   
</tx:advice>   
   
<bean id="hibernateTemplate"   class="org.springframework.orm.hibernate3.HibernateTemplate">   
       <property name="sessionFactory" ref="sessionFactory" />   
</bean>   
   
<bean id="persondao"   
       class="com.sms.freeborders.dao.hbimpl.PersonDAOImpl">   
    <property name="hibernateTemplate" ref="hibernateTemplate" />   
</bean>   

 

分享到:
评论

相关推荐

    详解Spring配置事务的五种方式

    本文将详细介绍Spring配置事务的五种方式,以便开发者们更好地理解和应用。 首先,Spring配置事务通常涉及三个关键组件: 1. **DataSource**:数据源,它是连接到数据库的桥梁,负责管理数据库连接。在使用...

    spring配置事务五种方式.doc

    本文将详细介绍Spring配置事务的五种方法,每种方法都基于相同的基本组件:DataSource、TransactionManager以及代理机制。理解这些配置方式有助于更好地控制事务在应用程序中的行为。 1. **每个Bean都有一个代理** ...

    spring 配置事务

    而Spring配置事务管理则是其中的关键技术,它确保了业务操作的一致性和数据的完整性。本篇文章将详细探讨Spring如何配置事务,以及如何在Spring与Hibernate集成的环境中实现事务管理。 首先,让我们了解什么是事务...

    spring配置事务

    这里我们将深入探讨Spring配置事务所涉及的知识点,以及与之相关的jar包。 首先,Spring提供了两种主要的事务管理方式: 1. **声明式事务管理**:这是通过在XML配置文件或注解中定义事务边界来实现的。这种方式...

    Spring配置事务在DAO层和业务逻辑层

    ### Spring配置事务在DAO层和业务逻辑层 #### 一、Spring事务管理概述 Spring框架提供了两种事务管理方式:编程式事务管理和声明式事务管理。其中,声明式事务管理是通过配置来完成的,它利用Spring AOP特性,将...

    Spring配置事务处理

    这是在java里使用到spring的配置文件里,添加事务处理过程,以至于可以回滚事务,当中使用到拦截器。

    Spring事务配置的五种方式

    Spring 事务配置是Spring框架中不可或缺的部分,它用于管理和协调应用程序中的事务边界,确保数据的一致性和完整性。在Spring中,事务配置主要涉及到三个核心组件:DataSource、TransactionManager以及代理机制。...

    ssh框架使用spring配置事务所需的jar包

    在Spring中配置事务管理,我们需要引入特定的jar包来实现AOP功能,这些正是`aopalliance.jar`、`aspectjrt.jar`和`aspectjweaver.jar`。 首先,`aopalliance.jar`是AOP Alliance项目提供的一个库,它定义了AOP领域...

    Spring配置JTA事务管理

    4. 配置事务属性:Spring提供了`@Transactional`注解来标记需要进行事务管理的方法。你可以设置事务的隔离级别、传播行为、超时时间等属性。 5. 使用事务:在业务逻辑中,你可以通过编程式或声明式的方式来使用事务...

    Spring AOP配置事务方法

    Spring AOP 配置事务方法 Spring AOP(Aspect-Oriented Programming,面向方面编程)是一种编程范式,它允许开发者在不修改源代码的情况下,增强和修改应用程序的行为。 Spring AOP 提供了一种灵活的方式来实现事务...

    spring事务配置的五种方式

    1. **基于XML的全局事务配置**:这种方式通过在Spring配置文件中设置`&lt;tx:advice&gt;`和`&lt;aop:config&gt;`元素来实现事务管理。相比于第一种方式,这种方式更加简洁,适用于所有业务逻辑Bean。 2. **基于注解的事务配置**...

    spring声明事务的配置

    为了克服上述问题,Spring 1.x引入了一种更推荐的方式,即通过基类来配置事务。你可以创建一个抽象的事务代理bean,如`baseTransactionProxy`,并在此处定义通用的事务属性。然后,你的业务bean可以继承这个基类,...

    spring3.0两种事务管理配置

    Spring 3.0 事务管理配置 Spring 3.0 提供了两种事务管理配置方法:基于 XML 的事务管理和基于 @Transactional 的事务管理,这两种方法都是为了实现事务管理的目标,分别具有不同的配置方式和优缺点。 基于 XML ...

    spring的事务管理配置

    spring的事务管理配置详解.txt详细的描述了如何配置spring的事物。

    spring声明式事务配置

    根据提供的信息,我们可以深入探讨Spring框架中的声明式事务配置及其多种实现方式。声明式事务管理是一种简化事务管理的方式,它允许开发人员通过配置而非编程来指定事务边界,从而减少了代码的复杂性并提高了可维护...

    spring事务与数据库操作

    ### Spring事务与数据库...- **配置事务规则**:最后,需要配置事务规则,指定哪些方法需要在事务中执行。 ##### 1.3 示例代码 下面是一个简单的示例,展示了如何使用XML配置文件来配置Spring的声明式事务: ```xml ...

    Spring 事务代理配置

    在深入探讨Spring事务代理配置之前,我们先简要回顾一下Spring...通过以上步骤,我们就可以在Spring中成功配置事务代理,使得应用程序中的业务方法能够自动进行事务管理,极大地提高了代码的可维护性和事务的一致性。

    Spring基于XML方式配置事务

    这里我们主要探讨的是"Spring基于XML方式配置事务",这涉及到Spring的事务管理器、事务属性以及如何在XML配置文件中定义这些元素。 首先,Spring的事务管理分为两种模式:编程式事务管理和声明式事务管理。编程式...

    实验 spring 声明事务

    -- 配置事务管理器 --&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;!-- 启用基于注解的事务管理 --&gt; &lt;!-- 定义事务策略 --&gt; *" ...

Global site tag (gtag.js) - Google Analytics