Spring中配置事务的几种方式

2024-09-04 04:32

本文主要是介绍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://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.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="root" /><!-- 连接池启动时的初始值 --><property name="initialSize" value="10" /><!-- 连接池的最大值 --><property name="maxActive" value="10" /><!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --><property name="maxIdle" value="20" /><!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --><property name="minIdle" value="10" /><property name="defaultAutoCommit" value="true" /></bean><!-- 会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mappingLocations"><list><value>classpath:/com/nms/entity/**/*.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></bean>    <!-- 定义事务管理器 -->  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置服务层 --><bean id="userDaoAgency" class="com.dao.impl.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="userDaoAgency" />  <property name="proxyInterfaces" value="com.dao.UserDao" /><!-- 配置事务属性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop></props>  </property>  </bean>
</beans>第二种:所有Bean共享一个代理
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- // 配置同上 --></bean><!-- 定义事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 定义事务 --><bean id="base" 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><!-- 配置服务层 --><bean id="userDao" class="com.dao.impl.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 代理对象 --><bean id="userDaoAgency" parent="base"><property name="target" ref="userDao" /></bean>
</beans>第三种:拦截器:
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- // 配置同上 --></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>*DaoImpl</value></list>  </property>  <property name="interceptorNames">  <list>  <value>transactionInterceptor</value>  </list>  </property>  </bean>  <!-- 配置服务层 --><bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean>
</beans>第四种:使用tx标签配置的拦截器:
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory"><!-- // 配置同上 --></bean><context:annotation-config /><context:component-scan base-package="com.dao" /><!-- 定义事务管理器 -->  <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.dao.*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /></aop:config>
</beans>第五种:注解:
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory"><!-- // 配置同上 --></bean><context:annotation-config /><!-- 使用注解的包路径 --><context:component-scan base-package="com.dao" /><!-- 支持  @Transactional 标记 --><tx:annotation-driven transaction-manager="transactionManager"/><!-- 定义事务管理器 -->  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean>
</beans>
如果使用了注解,那么实现类应该这样写:
@Transactional
@Component("userDaoAgency")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {/*** 为方法增加事务处理特性*/@Transactional(readOnly=true)public void getUser(){        }    
}
第一种:每个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://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.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="root" /><!-- 连接池启动时的初始值 --><property name="initialSize" value="10" /><!-- 连接池的最大值 --><property name="maxActive" value="10" /><!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --><property name="maxIdle" value="20" /><!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --><property name="minIdle" value="10" /><property name="defaultAutoCommit" value="true" /></bean><!-- 会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mappingLocations"><list><value>classpath:/com/nms/entity/**/*.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property></bean>    <!-- 定义事务管理器 -->  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置服务层 --><bean id="userDaoAgency" class="com.dao.impl.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="userDaoAgency" />  <property name="proxyInterfaces" value="com.dao.UserDao" /><!-- 配置事务属性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop></props>  </property>  </bean>
</beans>第二种:所有Bean共享一个代理
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- // 配置同上 --></bean><!-- 定义事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 定义事务 --><bean id="base" 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><!-- 配置服务层 --><bean id="userDao" class="com.dao.impl.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 代理对象 --><bean id="userDaoAgency" parent="base"><property name="target" ref="userDao" /></bean>
</beans>第三种:拦截器:
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- // 配置同上 --></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>*DaoImpl</value></list>  </property>  <property name="interceptorNames">  <list>  <value>transactionInterceptor</value>  </list>  </property>  </bean>  <!-- 配置服务层 --><bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean>
</beans>第四种:使用tx标签配置的拦截器:
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory"><!-- // 配置同上 --></bean><context:annotation-config /><context:component-scan base-package="com.dao" /><!-- 定义事务管理器 -->  <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.dao.*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /></aop:config>
</beans>第五种:注解:
<?xml version="1.0" encoding="UTF-8"?>
<beans><!-- 数据源 --><bean id="dataSource"><!-- // 配置同上 --></bean><!-- 会话工厂 --><bean id="sessionFactory"><!-- // 配置同上 --></bean><context:annotation-config /><!-- 使用注解的包路径 --><context:component-scan base-package="com.dao" /><!-- 支持  @Transactional 标记 --><tx:annotation-driven transaction-manager="transactionManager"/><!-- 定义事务管理器 -->  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean>
</beans>
如果使用了注解,那么实现类应该这样写:
@Transactional
@Component("userDaoAgency")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {/*** 为方法增加事务处理特性*/@Transactional(readOnly=true)public void getUser(){        }    
}

这篇关于Spring中配置事务的几种方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135001

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u