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

 更新时间:2016年06月15日 14:22:59   投稿:lijiao  
这篇文章主要为大家详细介绍了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.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(); 
 } 
... 
}

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

相关文章

  • 详解SpringBoot封装使用JDBC

    详解SpringBoot封装使用JDBC

    这篇文章主要介绍了SpringBoot封装JDBC使用教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Spring @ComponentScan注解扫描组件原理

    Spring @ComponentScan注解扫描组件原理

    这篇文章主要介绍了Spring @ComponentScan自动扫描组件使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • Ribbon单独使用,配置自动重试,实现负载均衡和高可用方式

    Ribbon单独使用,配置自动重试,实现负载均衡和高可用方式

    这篇文章主要介绍了Ribbon单独使用,配置自动重试,实现负载均衡和高可用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 微信公众平台(测试接口)准备工作

    微信公众平台(测试接口)准备工作

    想要微信开发,首先要有个服务器,但是自己没有。这时候可以用花生壳,将内网映射到公网上,这样就可以在公网访问自己的网站了。
    2016-05-05
  • Spring Boot中使用Redis和Lua脚本实现延时队列的方案

    Spring Boot中使用Redis和Lua脚本实现延时队列的方案

    通过使用Redis和Lua脚本,可以在Spring Boot环境中实现一个高效且可靠的延时队列系统,这种方法利用了Redis的有序集合数据结构和Lua脚本的原子性操作来确保任务的正确性和一致性,这篇文章主要介绍了Spring Boot中使用Redis和Lua脚本实现延时队列,需要的朋友可以参考下
    2024-05-05
  • Java ApiPost请求返回406状态码问题的解决方案

    Java ApiPost请求返回406状态码问题的解决方案

    APIPost是一款专为开发者和测试人员设计的API测试工具,类似于Postman,但提供了更多的团队协作和文档管理功能,它可以帮助你更好地进行接口调试和集成测试,但遇到了请求后返回的是406状态,所以本文给大家介绍了Java ApiPost请求返回406状态码问题的解决方案
    2025-04-04
  • Java锁竞争导致sql慢日志原因分析

    Java锁竞争导致sql慢日志原因分析

    这篇文章主要介绍了Java锁竞争导致sql慢的日志原因分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-11-11
  • springboot如何获取request请求的原始url与post参数

    springboot如何获取request请求的原始url与post参数

    这篇文章主要介绍了springboot如何获取request请求的原始url与post参数问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java实现抢红包功能

    Java实现抢红包功能

    这篇文章主要为大家详细介绍了Java实现抢红包功能,采用多线程模拟多人同时抢红包,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • SpringBoot bean查询加载顺序流程详解

    SpringBoot bean查询加载顺序流程详解

    当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成自动装配时。你会声明自己的Configuration类,但是可能你面对的是好几个有互相依赖的Bean
    2023-03-03

最新评论