首页 > 其他分享 >Spring事务配置的五种方式

Spring事务配置的五种方式

时间:2023-06-04 12:32:42浏览次数:40  
标签:事务 http Spring www springframework bean 五种 org schema


Spring事务配置的五种方式


    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。

    总结如下:

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

    DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

    具体如下图:

Spring事务配置的五种方式_encoding

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

}


 


标签:事务,http,Spring,www,springframework,bean,五种,org,schema
From: https://blog.51cto.com/u_13538361/6410378

相关文章

  • Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part4 (完)
     Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring)part4----addcairngorm1.添加在libs下添加Cairngorm.swc,此时,具备了cairngorm框架能力2.运用cairngorm框架2.1在flex_src下创建如下文件夹和文件   AdminVO.aspackagevo.AdminVO{ [Bindable] publicclassAdm......
  • Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring) part3
    Flex+J2EE实例(cairngorm+blazeDS+hibernate+spring)part3                                   ----addspring &hibernate1.在WEB-INF\lib下添加spring、hibernate常用jar,包括mysql数据库的连接驱动  2.在mysql数据库中创建数据库......
  • 在 Spring 中自定义 scope
    大家对于Spring的scope应该都不会默认。所谓scope,字面理解就是“作用域”、“范围”,如果一个bean的scope配置为singleton,则从容器中获取bean返回的对象都是相同的;如果scope配置为prototype,则每次返回的对象都不同。一般情况下,Spring提供的scope都能满足日常应用的......
  • Spring 的狭义与广义
    Java开发者对于Spring应该不会陌生。Spring可以说是JavaEE开发事实上的标准。无论是Web开发,还是分布式应用,Spring都致力于简化开发者创建应用的复杂性。本文讨论Spring在狭义上以及广义上,所承载的不同的概念。Spring有广义与狭义之说。狭义上的Spring——SpringFram......
  • Spring RestTemplate 调用天气预报接口乱码的解决
    SpringRestTemplate调用天气预报接口可能遇到中文乱码的问题,解决思路如下。问题出现我们在网上找了一个免费的天气预报接口http://wthrcdn.etouch.cn/weather_mini?citykey=101280601。我们希望调用该接口,并将返回的数据解析为JSON格式。核心业务逻辑如下:privateWeatherRespo......
  • 踩坑|以为是Redis缓存没想到却是Spring事务!
    前言  最近碰到了一个Bug,折腾了我好几天。并且这个Bug不是必现的,出现的概率比较低。一开始我以为是旧数据的问题,就让测试重新生成了一下数据,重新测试。由于后面几轮测试均未出现,我也就没太在意。  可惜好景不长,测试反馈上次的问题又出现了。于是我立马着手排查,根据日志的表......
  • 聊聊Spring Cloud Gateway
    网关概述整体来看,网关有点类似于门面,所有的外部请求都会先经过网关这一层。网关不仅只是做一个请求的转发及服务的整合,有了网关这个统一的入口之后,它还能提供以下功能。针对所有请求进行统一鉴权、限流、熔断、日志。协议转化。针对后端多种不同的协议,在网关层统一处理后以HT......
  • (五)Spring源码解析:ApplicationContext源码解析
    一、概述1.1>整体概览在前面的内容中,我们针对BeanFactory进行了深度的分析。那么,下面我们将针对BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与BeanFactory的功能相似,都是用于向IOC中加载Bean的。由于ApplicationConext的功能是大于BeanFactory的......
  • 《面试1v1》Spring循环依赖
    我是javapub,一名Markdown程序员从......
  • 《面试1v1》SpringMVC
    我是javapub,一名Markdown程序员从......