- 浏览: 174467 次
- 性别:
- 来自: 郑州
-
文章分类
- 全部博客 (90)
- web前端 (23)
- java (30)
- 其它 (7)
- jbpm4.4 (1)
- 服务器 (2)
- 数据库 (11)
- J2se (1)
- 812202 (0)
- access sql (1)
- 数据库管理工具 (1)
- smartupload (1)
- win7 控制台窗口已停止工作 (1)
- xfire spring web service (1)
- 免安装版本 tomcat 后台启动 设置 (1)
- js 处理日期类型 (1)
- input 自动填充 (1)
- android 连接webservice (2)
- tomcat 直接通过IP访问web项目 (1)
- svn 修改历史日志 (1)
- java 设计模式之适配器 (1)
- android 程序日志记录 (3)
- Win7 电脑突然USB无法识别 方法记录 (1)
- java 实现图片缩略 (1)
- android 更多,刷新 (3)
- access 导入mysql (1)
- java 加密,解密 (0)
- 闪回数据 (1)
- extjs 弹出进度条 (1)
- JSP 绝对路径显示服务器非webapps下的图片或资源 (0)
- html 制作彩信未完 (1)
- android 2.2 api (1)
- java 反射 (1)
- jquery 购物车 (1)
最新评论
-
masuweng:
oracle 迁移 sqlite -
nanjiwubing123:
你好 ,在IE6环境下运行demo,出现dhtmlXTreeO ...
dhtmlxtree 右键菜单的增加,删除,修改 -
李_俊:
呃,那么复杂,果断无视,找一个简单的。
java 源码混淆器 -
shanshan518:
兼容性不好,还有没有精确到秒,楼主
精确到秒的js日期时间选择器 -
mynamelzk:
日期时间选择器非常好用,谢谢你了
日期选择器,中文
转载http://developer.51cto.com/art/200906/130499.htm
Spring配置文件中关于Spring事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。 DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。 具体如下图:
根据代理机制的不同,总结了五种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(); } }
发表评论
-
Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换
2013-07-16 19:15 0转载http://my.oschina.net/sorthW ... -
Spring中加载ApplicationContext.xml文件的方式【转】
2013-03-18 09:34 10631.利用ClassPathXmlApplicationCon ... -
Duplicate name in Manifest: SHA1-Digest
2012-11-27 17:17 4056Duplicate name in Manifest: SHA ... -
JSP 绝对路径显示服务器非webapps下的图片或资源
2012-05-08 19:08 0设置tomcate 虚拟路径 1:在tomcate/ser ... -
java 加密,解密
2012-04-05 17:20 0package test; impor ... -
java 实现图片缩略
2012-02-29 11:02 1024用到的Jar:Thumbnails 查看下载 地址:http ... -
java 设计模式之适配器
2012-02-20 10:48 949转载:http://www.lshine.com/index. ... -
tomcat 直接通过IP访问web项目
2012-02-19 12:28 48051:在开发时访问web项目是这样的http://localho ... -
xfire ibatis spring web service
2011-11-04 14:37 12911:所用的jar xfire-all-1.2.6.ja ... -
xfire spring web service
2011-11-03 18:17 1154转载 http:// ... -
Spring_ibatis_jta多数据源配置
2011-08-12 15:02 2078转载 Spring+iBatis+JOTM实现JTA事 ... -
java.lang.UnsatisfiedLinkError解决方法汇集(转载)
2011-08-04 16:59 18872转载http://blog.csdn.net/niux ... -
JFileChooser使用详解
2011-06-21 10:38 4907JFileChooser 转载 (2010-11-30 23 ... -
java 连接各种数据库 总结
2011-05-24 23:09 4579Oracle8/8i/9i数据库(thin模式) Cl ... -
java 实现某个日期增减天数
2011-05-20 01:46 1370转载:http://blog.csdn.net/zdwzzu2 ... -
dom4j 验证xml dtd
2011-05-08 18:52 1130转载:http://hi.baidu.com/%C0%EE%D ... -
Eclipse 3.WindowBuilder Pro及使用SWT Designer
2011-05-06 11:59 1767转载:http://apps.hi.baidu.com ... -
swing 管理器二
2011-05-06 09:26 936import java.awt.Dimension; imp ... -
swing 布局管理器
2011-05-05 17:53 1189当选择使用JPanel和顶层容器的content pane时 ... -
java 源码混淆器
2011-04-19 16:41 12649[转载] http://blueskylan.itey ...
相关推荐
### Spring事务配置的五种方式详解 #### 一、引言 在企业级应用开发中,事务处理是非常重要的一部分,特别是在涉及多个数据库操作时。Spring框架提供了强大的事务管理功能,支持编程式和声明式两种事务处理方式。...
以下是对Spring事务配置五种方式的详细解释: 1. **基于代理的事务管理(Proxy-based Transaction Management)** 这是第一种配置方式,每个Bean都有一个独立的代理。在示例代码中,`TransactionProxyFactoryBean`...
以下是对Spring事务配置五种方式的详细解释: 1. **基于注解的事务管理(@Transactional)** 使用`@Transactional`注解在类或方法级别声明事务边界。这种方式适用于基于Java的配置,注解可以明确指定事务的传播...
Spring事务原理是指Spring框架中的一种机制,用于管理事务,并提供了多种配置方式。事务是指一系列的操作,作为一个整体执行,如果其中某个操作失败,整个事务将回滚。Spring事务原理围绕着两个核心:...
Spring框架在处理事务时提供了五种不同的配置方式,这些配置主要涉及到事务的声明式管理和编程式管理。在Spring中,事务管理通常分为三部分:DataSource、TransactionManager和代理机制。DataSource是数据源,...
下面将详细介绍Spring的五种事务配置方式。 1. **基于XML的事务配置** - **每个Bean都有一个代理**: 在这种配置方式中,每个需要事务管理的Bean都会有一个对应的代理Bean。例如,对于DAO层的UserDao,我们首先...
Spring事务配置的五种方式 Spring框架中事务配置是非常重要的一部分,通常由三个组成部分组成,即DataSource、TransactionManager和代理机制。无论采取何种配置方式,代理机制部分总是变化的,而DataSource和...
本文将详细介绍Spring配置事务的五种方法,每种方法都基于相同的基本组件:DataSource、TransactionManager以及代理机制。理解这些配置方式有助于更好地控制事务在应用程序中的行为。 1. **每个Bean都有一个代理** ...