事务管理(下) 配置spring事务管理的几种方式(声明式事务)
配置spring事务管理的几种方式(声明式事务)
概要:
Spring对编程式事务的支持与EJB有很大的区别。不像EJB和Java事务API(Java Transaction API, JTA)耦合在一起那样,Spring通过回调机制将实际的事务实现从事务性的代码中抽象出来。实际上,Spring对事务的支持甚至不需要JTA的实现。如果你的应用程序只使用一种持久化资源,Spring可以使用持久化本事所提供的事务性支持,这包括了JDBC,Hibernate以及Java持久化API(Java Persistence API,JPA)。但是如果应用程序的事务跨多个资源,那么Spring会使用第三方的JTA实现来支持分布式(XA)事务。
编码式事务允许用户在代码中精确定义事务的边界,而声明式事务(基于AOP)有助于用户将操作与事务规则经行解耦。无论选择将事务编码到Bean中还是将选择其定义为切面,我们都需要使用Spring事务管理器与平台相关的事务经行交互。(也就是无论选择编码式还会声明式,都需要在XML中定义事务管理器)。
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。
具体如下图:
根据4种代理机制的不同,总结了五种Spring事务的配置方式。
目录:
- Bean和代理
- 使用拦截器
- 使用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"
- 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="hibernateTransactionManager"
- 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>
第一种,使用tx标签方式

- <!-- 第一种配置事务的方式 ,tx-->
- <tx:advice id="txadvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
- <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception" />
- <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
- <tx:method name="*" propagation="REQUIRED" read-only="true"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="daoMethod" expression="execution(* com.dao.*.*(..))"/>
- <aop:advisor pointcut-ref="daoMethod" advice-ref="txadvice"/>
- </aop:config>

expression="execution(* com.dao.*.*(..))"
其中第一个*代表返回值,第二*代表dao下子包,第三个*代表方法名,“(..)”代表方法参数。
第二种,使用代理方式

- <!-- 第二种配置事务的方式 ,代理-->
- <bean id="transactionProxy"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
- <property name="transactionManager" ref="transactionManager"></property>
- <property name="transactionAttributes">
- <props>
- <prop key="add*">PROPAGATION_REQUIRED, -Exception</prop>
- <prop key="modify*">PROPAGATION_REQUIRED, -Exception</prop>
- <prop key="del*">PROPAGATION_REQUIRED, -Exception</prop>
- <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id="userDao" parent="transactionProxy">
- <property name="target">
- <!-- 用bean代替ref的方式-->
- <bean class="com.dao.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- </property>
- </bean>

将transactionProxy的abstract属性设置为"true",然后将具体的Dao的parent属性设置为"transactionProxy",可以精简代码。
第三种,使用拦截器

- <!-- 第三种配置事务的方式,拦截器 (不常用)-->
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager"></property>
- <property name="transactionAttributes">
- <props>
- <prop key="add*">PROPAGATION_REQUIRED, -Exception</prop>
- <prop key="modify*">PROPAGATION_REQUIRED, -Exception</prop>
- <prop key="del*">PROPAGATION_REQUIRED, -Exception</prop>
- <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id="proxyFactory" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- <property name="beanNames">
- <list>
- <value>*Dao</value>
- </list>
- </property>
- </bean>

Bean和代理
第一种方式:每个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>
总结:
- 定义数据源
- 定义事务管理器(将数据源加入)
- 定义Dao Bean(将数据源加入)
- 定义TransactionProxyFactoryBean代理Bean,将事务管理器,Dao Bean加入。
第二种方式:所有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="hibernateTransactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="HibernateProxyTemplate"
- abstract="true"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <!-- 配置事务管理器 -->
- <property name="transactionManager">
- <ref bean="hibernateTransactionManager"/>
- </property>
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="create*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="reg*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="apply*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="add*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="remove*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="update*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="delete*">
- PROPAGATION_REQUIRED,-java.lang.Exception
- </prop>
- <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean id="userDao" class="edu.fjnu.hotelsys.dao.UserDaoHibernateImpl">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <bean id="userServiceTarget" class="edu.fjnu.hotelsys.service.UserServiceImpl">
- <property name="userDao" ref="userDao"></property>
- </bean>
- <bean id="userService" parent="HibernateProxyTemplate">
- <property name="target">
- <ref bean="userServiceTarget"/>
- </property>
- </bean>
只要指明它的parent(父类)就可以了。父类一般把abstract="true",因为在容器加载的时候不需要初始化,等到用的时候再有它的子类调用的时候,再去初始化。
总结:
- 定义数据源
- 定义事务管理器(将数据源加入),且将事务管理器声明为abstract="true"
- 定义Dao Bean(将数据源加入)
- 定义TransactionProxyFactoryBean代理Bean,将事务管理器,Dao 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="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://www.cnblogs.com/longshiyVip/p/5061547.html
事务管理(下) 配置spring事务管理的几种方式(声明式事务)的更多相关文章
- 配置spring事务管理的几种方式(声明式事务)
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- spring整合mybatis,ioc容器及声明式事务配置
步骤: 1.创建jdbc.properties文件,用来管理存放连接数据库的相关信息 jdbc.properties:jdbc.user=root jdbc.password=123456 jdbc. ...
- Spring框架(三) JDBCTemplate,声明式事务,自动装载(注解)
JDBCTemplate 按照标准正常项目的结构 结构图: model层 dao层 实现 dao:(DateBase Access Object) 数据访问对象,dao层只用来访问数据库和模型层 s ...
- Spring注解驱动开发(四)-----aop、声明式事务
AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...
- 第一课:Centos下配置java环境变量的两种方式(jdk1.8)
配置java环境(yum安装) 1.查出java1.8的全部版本 yum list java-1.8* 2.安装你需要的java1.8 版本(安装的名字根据查询出来的结果输入这里只是举例) yum i ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- spring事务管理——编程式事务、声明式事务
本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...
- Spring编程式事务管理及声明式事务管理
本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...
随机推荐
- 2.2 C#的注释
注释,是代码中的一些“说明文档”,注释注释本身不会参与程序代码的编译和运行,仅仅是为了方便程序员阅读. C#的注释分为:单行注释.多行注释.文档注释. 单行注释的符号是2条斜杠线(斜线的方向是左下到右 ...
- android手机和ios手机的分辨率
Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3 VGA 640*480 (Video Graphics Array) QVGA 320*240 (Quarter VGA ...
- linux通过ntp设置系统时间
1.查看本机时间 date 2.安装ntp并且设置开机启动 sudo yum -y install ntp chkconfig ntp on 3.立即更新系统时间 sudo ntpdate time. ...
- Python字符串格式化
一.使用格式化符来格式化字符串: Python支持的所有格式化符 格式化符 意义 'd' 返回要格式化对象的十进制表示,如果可以 'i' 返回要格式化对象的十进制表示,如果可以 'o' 返回要格式化对 ...
- linux hadoop安装
linux hadoop安装 本文介绍如何在Linux下安装伪分布式的hadoop开发环境. 在一开始想利用cgywin在 windows下在哪, 但是一直卡在ssh的安装上.所以最后换位虚拟机+ub ...
- The constructor BASE64Encoder() is not accessible due to restriction on required library
在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示:Access restriction : T ...
- Qt qml treeview 树控件
qml并没有提供树控件,只能自己写了.model仍然用ListModel对象,弄成层级的就行.delegate必须用loader动态的增加子控件,如此而已. [先看效果] [下载] http://do ...
- django 其他地址访问不了问题
启动的时候 使用 python manage.py runserver 0.0.0.0:8000 即可接收所有IP访问
- Xcode Build Settings Architectures
http://foggry.com/blog/2014/05/09/xcodeshe-zhi-xiang-zhi-architectureshe-valid-architectures/ https: ...
- HashMap与ConcurrentHashMap的区别
从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...