SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式
这对时间在学习SSH中Spring架构,Spring的事务配置做了具体总结。在此之间对Spring的事务配置仅仅是停留在听说的阶段,总结一下。总体把控。通过这次的学习发觉Spring的事务配置仅仅要把思路理清,还是比較好掌握的。
总结例如以下:
Spring配置文件里关于事务配置总是由三个组成部分,各自是DataSource、TransactionManager和代理机制这三部分。不管哪种配置方式。一般变化的仅仅是代理机制这部分。
DataSource、TransactionManager这两部分仅仅是会依据数据訪问方式有所变化,比方使用Hibernate进行数据訪问时。DataSource实际为SessionFactory。TransactionManager的实现为HibernateTransactionManager。
详细例如以下图:
依据代理机制的不同,总结了五种Spring事务的配置方式,配置文件例如以下:
第一种方式:每一个Bean都有一个代理
<span style="font-size:18px;"><span style="font-size:18px;"><?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></span></span>
另外一种方式:全部Bean共享一个代理基类
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?
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></span></span></span>
第三种方式:使用拦截器
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://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></span></span></span>
第四种方式:使用tx标签配置的拦截器
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://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></span></span></span>
第五种方式:全注解
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?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></span></span></span>
此时在DAO上需加上@Transactional注解,例如以下:
<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">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();}}</span></span></span>
总结
XML配置方式
长处:
1. XML配置方式进一步减少了耦合,使得应用更加easy扩展。即使对配置文件进一步改动也不须要project进行改动和又一次编译。
2.在处理大的业务量的时候,用XML配置应该更加好一些。
由于XML更加清晰的表明了各个对象之间的关系,各个业务类之间的调用。同一时候spring的相关配置也能一目了然。
当然,有人会说,用XML配置,在大的业务量时候会使得XML文件过大,不easy查看。
这一点我们全然能够利用业务分解书写多个XML配置文件就能够了。
缺点:
配置文件读取和解析须要花费一定的时间,配置文件过多的时候难以管理。无法对配置的正确性进行校验,添加了測试难度。
annotation配置的优缺点:
长处:
1. 在class文件里。能够减少维护成本,annotation的配置机制非常明显简单
2. 不须要第三方的解析工具,利用java反射技术就能够完毕任务
3. 编辑期能够验证正确性,差错变得easy
4. 提高开发效率
缺点:
1. 假设须要对于annotation进行改动,那么要又一次编译整个project
2. 业务类之间的关系不如XML配置那样easy把握。
3. 假设在程序中annotation比較多,直接影响代码质量。对于代码的简洁度有一定的影响。
SSH内容非常多。先从宏观把控。在项目中历练……多多总结、思路清晰;为了提高系统的灵活性与减少维护成本,提高效率使用到了非常多的配置文件来回调用、返回。涉及到非常多联系,这个时候真的有必要绘图。一切尽在图中展示吧,画出自己的思维方式。
SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式相关推荐
- Spring事务配置的五种方式 说明
Spring事务配置的五种方式 [转 http://blog.csdn.net/hjm4702192/article/details/17277669] Spring配置文件中关于事务配置总是由三个 ...
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别、不可重复读与幻读的区别
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spr ...
- spring事务配置的两种方式
spring所有的事务管理策略类都继承自org.springframework.transaction.PlatformTransactionManager接口. <!-- 事务管理器配置,单数 ...
- 配置spring事务管理的几种方式(声明式事务)
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- spring中事务配置的3种方式-2
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch09s05.html http://zpchen.iteye.com/blog/ ...
- spring事务管理的两种方式
一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...
- spring事务实现的几种方式
一.前言 1..事务几种实现方式 (1)编程式事务管理对基于 POJO 的应用来说是唯一选择.我们需要在代码中调用beginTransaction().commit().rollback()等事务管理 ...
- Spring事务管理之几种方式实现事务
1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...
- 将Bean放入Spring容器中的五种方式
欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/weixin_43741092/ article/details/120176466 将bean放 ...
最新文章
- 磁盘加密软件TrueCrypt知识大全(三)之加密非系统分区/设备
- 1208. 尽可能使字符串相等
- 排列(permutation)的末尾 0 的个数
- 手把手教你实现热更新功能,带你了解 Arthas 热更新背后的原理
- 房地产“产权分割制”是什么大杀器?
- 4、EPM ——Smart View介绍和使用
- 2020年运营版双端直播盒子APP带引导安装 QQ微信一键登录+多级分销+粉色系列
- enovia之Spinner开发
- Win10以太网属性空白怎么回事
- json 微信小程序 筛选_微信小程序(同城小程序)_总结二(筛选功能)
- 【毕业设计】大数据淘宝用户行为数据分析与可视化 - flink
- 只有浏览器显示找不到服务器dns
- 旧金山市交通系统遭勒索软件感染细节进一步公布
- 用自建kinetics-skeleton行为识别数据集训练st-gcn网络流程记录
- Mybatis关系映射一对一的关系
- 牛客网小bai月赛40
- 2D横版跳跃游戏第一节
- SIM800C--实现正常通话
- 看雪论坛做测试题得30Kx(附答案)
- PTA习题集-团体程序设计天梯赛L1-003