欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-12 13:53:02
spring配置文件中关于事务配置总是由三个组成部分,分别是 datasource 、transactionmanager  和 代理机制 这三部分,无论哪种配置...

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

datasource、transactionmanager这两部分只是会根据数据访问方式有所变化,比如使用hibernate进行数据访问时,datasource实际为sessionfactory,transactionmanager的实现为hibernatetransactionmanager。

具体如下图:

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

根据代理机制的不同,总结了五种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.mktao.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(); 
 } 
... 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。