Spring事物配置的五种方式

TI DSP双工程实战:用CCS7.3在F28377D一片FLASH里‘装’两个程序 本文详细介绍了如何在TI DSP TMS320F28377D的片上FLASH中利用CCS7.3开发环境实现双工程共存。通过合理规划FLASH扇区和配置工程属性,开发者可以同时驻留稳定的底层代码和频繁更新的应用算法,显著提升工业控制器等嵌入式系统的开发效率和可靠性。 阅读详情

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

    总结如下:

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

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

    具体如下图:

Spring事务配置 (2)

    根据代理机制的不同,总结了五种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();
    }
    
    
}
SAP Business One自定义表与字段实战:从零构建企业专属数据模型 本文详细介绍了如何在SAP Business One中创建自定义表与字段,从零构建企业专属数据模型。通过实战案例和避坑指南,帮助用户掌握业务适配、流程优化和权限隔离等关键技巧,提升系统灵活性和数据管理效率。特别适合需要扩展标准功能的SAP Business One用户。 阅读详情

相关推荐

解放双手:用KUKA示教器白键一键触发复杂工艺,自定义你的快捷指令

本文详细介绍了如何利用KUKA示教器的工艺白键功能,通过自定义快捷指令一键触发复杂工艺,显著提升自动化产线效率。文章涵盖UserTech技术栈解析、KRL脚本封装、人机交互视觉工程及工业实战策略,帮助工程师实现机械臂操作的高阶优化。

weixin_30379531的博客 436

spring事务配置的五种方式

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

yygr的博客 3844

PICO4 VR开发调试神器:用Live Preview Plugin实现电脑串流,告别反复打包烧录的繁琐流程

本文详细介绍了PICO4 VR开发中如何使用Live Preview Plugin实现电脑串流,显著提升开发效率。通过实时预览和调试功能,开发者可以避免反复打包烧录的繁琐流程,特别适用于Unity环境下的VR应用开发。文章涵盖环境配置、连接设置、实时调试技巧及性能优化方案,是PICO4开发者的必备指南。

weixin_34110970的博客 427

Spring事务失效

[问题]       Spring的声明式事务,我想就不用多介绍了吧,一句话“自从用了Spring AOP啊,事务管理真轻松啊,真轻松;事务管理代码没有了,脑不酸了,手不痛了,一口气全配上了事务;轻量级,测试起来也简单,嘿!”。不管从哪个角度看,轻量级声明式事务都是一件解放生产力

zrlwoaini的专栏 4646

ADC(三)Guard ring的设计艺术:从单元到系统的隔离策略

本文深入探讨了ADC设计中Guard ring(保护环)从单元到系统的多层次隔离策略。文章分享了从新手到专家的认知升级过程,详细分析了在单元内部进行微隔离、在模块间建立噪声屏障以及在系统层面规划全局隔离带的实用方法与设计艺术,强调了Guard ring对于提升混合信号电路性能和可靠性的关键作用。

weixin_28312391的博客 165

ESP32连接PDM麦克风操作指南:新手入门必看

手把手教你如何将ESP32与PDM麦克风连接,采集音频数据并用于音频分类应用。内容涵盖硬件接线、代码配置及实际调试技巧,适合初学者快速上手esp32在声音识别场景中的使用。

weixin_42590539的博客 1343

Spring事务

Spring 管理事务的⽅式有⼏种? 1. 编程式事务,在代码中硬编码。(不推荐使⽤) 2. 声明式事务,在配置⽂件中配置(推荐使用) 声明式事务⼜分为两种: 1. 基于XML的声明式事务 2. 基于注解的声明式事务 Spring 事务中哪⼏种事务传播⾏为? ⽀持当前事务的情况: 1. TransactionDefinition.PROPAGATION_REQUIRED: 如果当前存在事务,则加⼊该事务;如果当前没有事务,则创建⼀个新的事务。 2. Transaction.

qq_38197844的博客 226

AI使能的SVD算子:基于深度学习的矩阵分解方法

奇异值分解(Singular Value Decomposition, SVD)是线性代数中一种重要的矩阵分解方法,广泛应用于信号处理、数据降维、推荐系统等领域。传统SVD算法虽然精确,但在处理大规模矩阵时面临计算复杂度高、内存需求大等问题。本文提出了一种基于深度学习神经网络的SVD算子实现方法,完全摒弃传统数值算法,仅使用PyTorch框架构建的神经网络模型来完成大规模信道矩阵的SVD分解与还原。

huanghm88的专栏 1364

springMVC 事物配置的5种方式

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34417814的博客 174

目标检测YOLO实战应用案例100讲-基于激光雷达点云的三维目标检测算法研究

近几年来,在计算机视觉领域,利用深度学习卷积神经网络技术来实现各种任务的算法越来越多。毫米波雷达、相机、激光雷达、超声波雷达等车载传感器生产成本的降低,汽车的自动驾驶技术得到了飞速发展,在我们的日常生活中经常能看到该技术辅助汽车驾驶员进行行车。和非自动驾驶汽车相比,自动驾驶汽车存在诸多优点,如进一步减少交通事故,保障人身安全;提供安全合理的路线,避免交通拥堵;采用清洁无污染的电能作为汽车新能源,从而降低大气污染等。现如今,自动驾驶技术已经逐渐深入到各个领域并发挥着关键的作用。

qq_36130719的博客 3744

【100%通过率】华为od机试C卷【贪吃的猴子】Java&Go 实现

一只贪吃的猴子,来到一个果园,发现许多串香蕉排成一行,每串香蕉上有若干根香蕉。每串香蕉的根数由数组numbers给出。猴子获取香蕉,每次都只能从行的开头或者未尾获取,并且只能获取N次,求猴子最多能获取多少根香蕉。第一次获取香蕉为行的开头,第二次获取为行的末尾,因此最终根数为4+3 =7。全部获取所有的香蕉,因此最终根数为1+2+3 = 6。第二行为数组numbers的值每个数字通过空格分开。1:简单题目,两个指针分别指向头尾,逐个对比即可。第一行为数组numbers的长度。第三行输入为N,表示获取的次数。

MISAYAONE的博客 1449

spring事物配置的五种方式

Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理...

王强的专栏 224

时间序列—显著相关性和滞后性分析_python

本文讲述了两个时间序列(信号)的相关性分析,可以利用相关性分析进行特征筛选。此外本文还讲了怎么判断时间序列的滞后性的方法。

qq_34229228的博客 2万+

Spring五种事物配置方式

总结如下: Spring配置文件中关于Spring事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。 DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为Sessi

hweixing123的专栏 517

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

spring事物的五种配置方式

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。     DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问 时,DataSource实际为SessionFactory,

cnsu-cmh 326

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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:aop="http://w

远古大猛犸 735

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

spring五种事物的配置方式

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。     DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用hibernate进行数据访问时,DataSource实际为SessionFactory,...

zengshaotao的专栏 150

两串储能电池被动均衡方案:MP2642GR-Z与STM32F722VE实战

电池管理系统(BMS)中,串联电池组的容量失配会引发木桶效应,导致整组可用容量下降、循环寿命缩短。被动均衡通过电阻泄放高电压电芯能量,以简单电路实现电芯电压拉平,是两串小规模储能场景的高性价比选择。其技术价值在于以低成本和低复杂度保障电池组一致性,广泛应用于备用电源、储能柜等长期浮充系统。MP2642GR-Z前端芯片负责电芯电压采集与均衡开关控制,STM32F722VE主控运行均衡策略,二者构成完整的两串被动均衡方案,兼顾采集精度、隔离设计与散热可靠性。

weixin_34174105的博客 234

spring事物管理五种配置方式

Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。 总结如下: Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理...

weixin_34132768的博客 86

Spring配置事物的5种方式

Spring事务配置的五种方式       (原文地址:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html)     Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制

IT丶小污的博客 326

Spring事务配置的五种方式

前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。     总结如下:     Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一

东桥小草的博客 330

Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别?

Isolation 属性一共支持五种事务设置,具体介绍如下:  l          DEFAULT 使用数据库设置的隔离级别 ( 默认 ) ,由 DBA 默认的设置来决定隔离级别 .  2          READ_UNCOMMITTED,读未提交 会出现脏读、不可重复读、幻读 ( 隔离级别最低,并发性能高 )  3          READ_COMMITTED

seasons1225的博客 427
上一篇: java高并发解决方案
下一篇: Memcache问题集锦
xiao345617945
博客等级 码龄19年 2粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值