Spring事务配置的五种方式

Shader Graph实战:5分钟搞定Flow Map水体流动效果(含UV偏移避坑指南) 本文详细解析了如何使用Unity的Shader Graph工具快速实现Flow Map水体流动效果,涵盖基础原理、核心节点搭建、高级技巧及性能优化。特别针对UV偏移问题提供避坑指南,帮助开发者轻松打造专业级动态水面效果,适合游戏场景增强视觉表现。 阅读详情

以前看过事务,没去具体的学,今天面试又遇到这个问题,这是在网上看到的一篇好文章,加了一点自己的见解,分享给大家

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中属性method定于对于哪些方法定义什么样的规则,像以get开头的读取方法我们一般不加事务,事务也会影响性能,
     read-only定为只能读(其实可以不加),而其他的方法都要加事务-->  
    
<tx:advice id="txAdvice" transaction-manager="transactionManager">
        
<tx:attributes>
            
<tx:method name="*" propagation="REQUIRED" />
   <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
        </tx:attributes>
    
</tx:advice>
    <!--aop中的变量定义事务的处理对象,在哪个层次的类加事务 -->  
    
<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();
    }
    
    
}
C51单片机驱动ILI9341彩屏实战:从Proteus仿真到绘制自定义图案(附完整代码) 本文详细介绍了使用C51单片机驱动ILI9341彩屏的完整实战流程。内容涵盖硬件连接、8080并行接口时序模拟、寄存器初始化、屏幕方向设置以及基本图形绘制原理,并重点讲解了如何在Proteus仿真环境中搭建电路、调试代码,最终实现自定义图案的显示。文章提供了完整的代码示例,为嵌入式开发学习者和复古硬件爱好者提供了一套可行的解决方案。 阅读详情

相关推荐

5个免费股票数据API实测对比:从AkShare到BaoStock,哪个最适合你的AI量化项目?

本文深度评测了AkShare、BaoStock、yfinance、Alpha Vantage和EOD Historical Data这五个免费股票数据API,从数据质量、覆盖范围、更新频率、稳定性及易用性进行对比。针对AI量化项目,文章提供了详细的选型策略,帮助开发者根据项目阶段与需求,精准选择最适合的数据源,并强调了多源验证与数据清洗的重要性。

weixin_29290947的博客 5072

spring事务配置的五种方式

https://blog.youkuaiyun.com/m8396017/article/details/51615806 spring事务配置的五种方式 第一种方式:每个Bean都有一个代理 第二种方式:所有Bean共享一个代理基类 第三种方式:使用拦截器 第四种方式:使用tx标签配置的拦截器 第五种方式:全注解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只..

yygr的博客 3843

【实践指南】Wasserstein距离在电力系统鲁棒优化中的核心应用

本文深入探讨了Wasserstein距离在电力系统分布鲁棒优化中的核心应用。针对风电、光伏出力的不确定性,文章阐述了如何利用Wasserstein距离构建模糊集,将复杂的分布鲁棒优化问题转化为可求解的确定性模型,并提供了从理论到代码实现的完整实践指南,有效平衡了系统运行的经济性与鲁棒性。

weixin_29229261的博客 306

Spring的事务及五种配置方式

SSH框架风靡整个IT行业,而作为该框架中的管理员,Spring负责管理其他的框架,协调各个部分的工作。那么今天小编就带大家一起学习Spring的事务管理和不同的配置方式: *  Spring配置文件中关于事务配置的有三个部分:datasource,TransactionManager和代理机制,无论是哪种配置方式,一般的变化只是代理机制这部分; *   DataSource、Tr

GO BEYOND YOURSELF 2289

编码约定的自由度:重新思考Hamming码在实际工程中的灵活应用

本文探讨Hamming码在实际工程中的灵活应用,突破标准实现的思维定式。通过分析不同编码约定的优缺点和资源受限环境下的设计权衡,展示如何根据嵌入式系统、通信协议等具体需求定制汉明码实现,优化编解码效率并降低硬件开销。

perl8的博客 177

快手did did_gt edid的注册过程

接口 https://gdfp.ksapisrv.com/rest/infra/gdfp/report/kuaishou/android did 是本地生成的16进制 或者 获取的 android_id did_gt 是did生成时间戳 1595073601970 public static final Random f16237a = new Random(System.currentTimeMillis()); public static long m19668a() {  &n

HURUWO的技术博客 1万+

Spring配置事务的五种方式

Java事务的类型有三种: JDBC事务、可以将多个 SQL 语句结合到一个事务中。JDBC 事务的一个缺点是事务的范围局限于一个数据库连接。一个 JDBC 事务不能跨越多个数据库 JTA(Java Transaction API)事务、事务可以跨越多个数据库或多个DAO,使用也比较复杂。 容器事务。主要指的是J2EE应用服务器提供的事务管理,局限于EJB应用使用。 spring事...

山山来驰的博客 94

从DDPM到DDIM:深入解读《Denoising Diffusion Implicit Models》

DDIM发表在ICRL2021上,是DDPM重要的改进之一,能显著提高DDPM的样本质量、减少采样时间,并且能显式控制插值,已经被广泛应用到现在的Diffusion Models上。这篇博客和大家一起详细解读一下DDIM,认识这一伟大的模型。.........

沉迷单车的追风少年 1万+
上一篇: IT职场求生法则(3)- 部门经理及副总适用法则
下一篇: java解析xml文件
7star_java
博客等级 码龄15年 80粉丝 127原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值