Spring事务配置的五种方式

MySQL 8.0数据库崩了别慌!手把手教你仅凭一个.ibd文件找回数据(附Go工具包) 本文详细介绍了在MySQL 8.0数据库崩溃后,如何仅凭.ibd文件恢复数据的完整流程。通过使用Go语言工具包和ALTER TABLE技巧,即使没有备份也能实现数据重生。文章包含从环境准备到数据验证的全方位指导,帮助DBA和开发者应对紧急数据恢复场景。 阅读详情

http://zdq0426.blog.163.com/blog/static/2216909420101124863710/

 

    前段时间对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();
    }
   
   
}

使用IntelliJ IDEA配置版本管理(SVN和Git) 在开发过程中,版本管理是必不可少的一部分,它可以帮助团队协作、管理代码历史记录,并且提供了代码回滚和分支管理等功能。本文将介绍如何使用IntelliJ IDEA配置版本管理,包括SVN和Git两种方式,另外列举了常见的问题并指导修复,以帮助开发者更高效地进行代码管理和协作。 阅读详情

相关推荐

4D毫米波雷达

详解4D成像毫米波雷达

sangba2019的博客 2326

Hibetnate(网上学习资料转载)

Hibetnate(网上学习资料转载)

WYZSC的专栏 1303

损失函数大汇总(四十三)(Boundary Loss附公式推导与代码)

摘要 Boundary Loss是一种针对图像分割任务的边界优化损失函数,特别适用于小目标、细长结构和类别不平衡场景。它通过引入真实边界的有符号距离场(SDF),将边界距离优化问题转化为区域积分形式。与Dice Loss等区域重叠损失不同,Boundary Loss更关注预测边界与真实边界的精确对齐,而非整体区域重叠。其核心公式为像素级SDF值与预测概率的乘积求和,通过梯度调整使预测概率在真实边界内部升高、外部降低。该损失通常与区域型损失(如CE或Dice)联合使用,形成"区域分割+边界修正&qu

悦学共鸣,温柔以待,汇聚光芒,共同成长 323

spring事务配置五种方式

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

yygr的博客 3841

Spring AOP 实现通用 Token 自动刷新重试工具包

这套组件把 token 过期的识别、刷新、重试全部交给 AOP 切面处理,业务代码只需要添加注解,消除样板代码。对于系统对接 U9、EBS、IMS 等多外部系统场景,非常适合接入。

u014755700的博客 194

Spring AI RAG管道构建与优化

<think>我们只需要根据给定的内容生成摘要(≤150字)。给定内容较长,但核心是Spring AI RAG管道构建与优化,涉及文档加载、分割、嵌入、存储、检索、生成等。摘要应高度概括。 注意用户要求“根据以下内容,生成≤150字的文章摘要”。内容提供了代码等细节,但摘要需抓住要点。 写出摘要,字数≤150。</think>摘要:本文深入讲解Spring AI RAG管道的构建与优化,涵盖文档加载、分割、嵌入、存储、检索到生成的全流程。详细展示了多源文档加载(PDF、网页、JSON)、多种分割策略(按页、

BADAO_LIUMANG_QIZHI的博客 224

Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动

dm‑r2dbc 是面向达梦 DM8 的实验性 R2DBC 驱动,混合架构:Netty 异步网络传输层 + JDBC 驱动协议编解码复用。核心思路:不去逆向达梦私有协议二进制报文,直接复用达梦 JDBC 驱动内部已经成熟的编解码逻辑,通过反射替换 JDBC 内部默认阻塞 Socket 传输实现,替换为 Netty NIO 异步通道,实现真正异步非阻塞网络 IO。

jinyuttt的专栏 298

SpringBoot 3:入门与应用实战》第 11 章 嵌入式容器 阅读笔记 29

SpringBoot 3:入门与应用实战》第 11 章 嵌入式容器 阅读笔记 29

道友 532

SpringBoot 集成 JQuick-Curl:Spring 环境完整接入教程,第三方接口调用别再到处手写模板类

JQuick-Curl 本身并不依赖 Spring 才能使用,但现实情况是,大多数 Java 业务项目最终都跑在 SpringBoot 里。于是一个非常实际的问题就来了:这个框架在 Spring 环境中应该怎么组织,才不会变成又一套零散工具?如果你只是临时在某个 Service 里直接 new 代理对象,虽然能跑,但很快就会遇到重复创建、配置散落、调用方式不统一的问题。真正合理的做法,是把 JQuick-Curl 当成 SpringBoot 项目中的一个外部接口调用层。

PaoHaiJiao的博客 303

SpringBoot 各版本 Profile 多环境配置完整对比(2.x/ 2.4+ / 3.x/4.x

方式 1:拆分多个配置文件(企业项目通用,全版本兼容)application-{profile名}.yml/yamlapplication.yml:主公共配置文件,存放通用配置,指定激活哪一个环境application-dev.yml:开发环境application-test.yml:测试环境application-prod.yml:生产环境加载规则:一定会加载主文件,再加载被激活的环境配置文件;相同 key,环境配置覆盖主配置。该写法所有 SpringBoot 版本全部兼容,无版本差异。

m0_63611023的博客 168

SpringCloud---可观测性与监控:Actuator / Micrometer / Prometheus / Grafana 深度解析

Spring Cloud 可观测性与监控:Actuator / Micrometer / Prometheus / Grafana 深度解析

zhzhxian的博客 385

基于stm32h743单片机开发系统bootloader的USB接口方式IAP升级(USB DFU)软件源码.zip

基于stm32h743单片机开发系统bootloader的USB接口方式IAP升级(USB DFU)软件源码.zip

Proteus 8.9免安装版本、Proteus 8 Professional 8.9免安装版本

Proteus 8.9免安装版本、Proteus 8 Professional1、解压到D盘2、打开D盘的Proteus 8 Professional文件夹3、进入BIN文件夹4、打开LICENCE.EXE应用程序进行安装密钥5、选中左边的Mr Wang(123456789@qq.com)6、点击下面的Install7、点击“是”(两边都有Mr Wang(123456789@qq.com))8、关闭窗口9、找到当前文件夹的PDS.EXE,右击“发送” “桌面快捷方式”,关闭窗口10、在桌面,可以将(PDS.EXE - 快捷方式)命名为“Proteus 8 Professional”11、打开Proteus 8 Professional就可以用了

上一篇: js验证技术整理
下一篇: Spring整合Struts详解
生活真美好
博客等级 码龄26年 227粉丝 124原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值