1、TransactionProxyFactoryBean
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="TblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="TblUserDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--
注意这个属性, 详细意义请参考Spring文档中的CGLIB部分或者本章的 10.7.2参考资料部分, 必须为 true
使用CGLIB才不用强制编写DAO接口
-->
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="TblUserDAO" />
</property>
<property name="transactionAttributes">
<props>
<!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="TblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="TblUserDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--
注意这个属性, 详细意义请参考Spring文档中的CGLIB部分或者本章的 10.7.2参考资料部分, 必须为 true
使用CGLIB才不用强制编写DAO接口
-->
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="TblUserDAO" />
</property>
<property name="transactionAttributes">
<props>
<!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
2、TransactionInterceptor
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="tblUserService" class="com.angi.dao.service.TblUserService">
<property name="tblUserDAO">
<ref bean="tblUserDAO" />
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>tblUserService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</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>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="tblUserService" class="com.angi.dao.service.TblUserService">
<property name="tblUserDAO">
<ref bean="tblUserDAO" />
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>tblUserService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</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>
</beans>
3、AOP和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:p="http://www.springframework.org/schema/p"
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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="tblUserService" class="com.angi.dao.service.TblUserService">
<property name="tblUserDAO">
<ref bean="tblUserDAO" />
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 需要引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->
<aop:pointcut id="daoMethods"
expression="execution(* com.angi.dao.service.TblUserService.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>
<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="tblUserService" class="com.angi.dao.service.TblUserService">
<property name="tblUserDAO">
<ref bean="tblUserDAO" />
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 需要引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->
<aop:pointcut id="daoMethods"
expression="execution(* com.angi.dao.service.TblUserService.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>
<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
4、anotation
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 需要引入tx的命名空间 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="tblUserService" class="com.angi.dao.service.TblUserService">
<property name="tblUserDAO">
<ref bean="tblUserDAO" />
</property>
</bean>
<!-- 声明一个 Hibernate3 的事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 需要引入tx的命名空间 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="tblUserService" class="com.angi.dao.service.TblUserService">
<property name="tblUserDAO">
<ref bean="tblUserDAO" />
</property>
</bean>
<!-- 声明一个 Hibernate3 的事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
@Transactional
public void doTransaction() {
// step1 insert
TblUser tblUser1 = new TblUser();
tblUser1.setId(24);
tblUser1.setUsername("Angi12");
tblUser1.setPassword("Wang21");
tblUserDAO.save(tblUser1);
// step2 update
TblUser tblUser2 = tblUserDAO.findById(2);
tblUser2.setPassword(tblUser2.getPassword() + "a");
tblUserDAO.update(tblUser2);
// step3 insert
TblUser tblUser = new TblUser();
tblUser.setId(23);
tblUser.setUsername("Angi");
tblUser.setPassword("Wang");
tblUserDAO.save(tblUser);
}
public void doTransaction() {
// step1 insert
TblUser tblUser1 = new TblUser();
tblUser1.setId(24);
tblUser1.setUsername("Angi12");
tblUser1.setPassword("Wang21");
tblUserDAO.save(tblUser1);
// step2 update
TblUser tblUser2 = tblUserDAO.findById(2);
tblUser2.setPassword(tblUser2.getPassword() + "a");
tblUserDAO.update(tblUser2);
// step3 insert
TblUser tblUser = new TblUser();
tblUser.setId(23);
tblUser.setUsername("Angi");
tblUser.setPassword("Wang");
tblUserDAO.save(tblUser);
}
相关推荐
### 标题解读:Spring声明式事务配置 Spring框架提供了两种主要类型的事务管理方式:编程式事务管理和声明式事务管理。声明式事务管理通过XML配置或注解的形式定义事务边界,使得业务逻辑与事务控制分离。 ### ...
Spring声明式事务配置管理方法
#### 三、Spring3声明式事务配置示例解析 ##### 1. 配置文件基础结构 在Spring3中,声明式事务的配置主要包含以下几个关键部分: - **引入命名空间**:首先需要在`beans`元素中引入`context`和`tx`命名空间。 - *...
Spring提供了四种不同的方式来配置声明式事务,这使得事务管理更加灵活且易于维护。下面将详细介绍这四种方式。 1. **基于XML的TransactionProxyFactoryBean** 这是最常见的一种方式,通过`...
在Spring框架中,声明式事务管理是通过元数据(如XML配置或注解)来定义事务边界,使得开发者无需在业务代码中显式调用事务管理API,即可实现事务的控制。TransactionProxyFactoryBean是Spring提供的一种工具,用于...
在"spring声明式事务管理配置方式"中,主要涉及到以下几个关键知识点: 1. **Spring事务管理器(Transaction Manager)**: - Spring支持多种事务管理器,如DataSourceTransactionManager(用于JDBC事务)和...
在Spring中,声明式事务主要通过AOP(面向切面编程)实现,它允许我们在不修改业务代码的情况下,通过XML配置或Java配置,以及注解来控制事务的边界。 3. **XML配置事务** 在Spring的XML配置文件中,可以通过`...
在Spring框架中,声明式事务管理是实现事务处理...在博文"Spring使用XML配置声明式事务"中,作者详细讲解了每个步骤,并可能通过示例代码展示了如何实际应用这些配置,帮助读者更好地理解和掌握Spring声明式事务管理。
本实例将深入探讨如何在Spring中使用AOP来实现声明式事务配置。 一、Spring AOP基础 AOP允许我们在程序运行时动态地将代码插入到其他对象中,它主要用来处理那些具有横切关注点的问题,如日志、异常处理和事务管理...
当我们需要在应用程序中进行事务管理时,Spring提供了一种声明式的方式,使得事务配置更为简洁。本篇将详细介绍如何在Spring 3和Hibernate 4中通过注解来实现声明式事务管理。 首先,我们需要在项目中引入Spring和...
Spring 中的事务处理可以分为两种方式:声明式事务处理和编程式事务处理。声明式事务处理通过 AOP 的实现,把事务管理代码作为方面封装到业务代码中,使得事务管理代码和业务代码解藕。这使得事务管理变得更加灵活...
文件名为`Spring声明式事务处理-1.mht`到`Spring声明式事务处理-5.mht`,通过阅读这些文件,你将能够深入理解Spring声明式事务处理的各个方面,包括配置、使用场景、最佳实践以及常见问题的解决方法。
本主题将深入探讨Hibernate的编程式事务管理和Spring AOP的声明式事务管理,以及两者如何在实际项目中集成使用。 **Hibernate编程式事务管理** Hibernate作为流行的ORM(对象关系映射)框架,提供了对JDBC事务的...
在这个"spring声明式事务处理demo"中,我们将探讨如何在MyEclipse环境下实现这一功能。 首先,我们要理解Spring事务管理的两种主要方式:编程式事务管理和声明式事务管理。编程式事务管理通常通过AOP(面向切面编程...
Spring_Hibernate使用TransactionInterceptor声明式事务配置 在这篇文章中,我们将探讨使用Spring_Hibernate框架实现声明式事务配置的方法,具体来说,就是使用TransactionInterceptor来管理事务。在Spring框架中,...
Spring 2.x版本的声明式事务配置模板是开发者常用的一种方式,它通过AOP(面向切面编程)实现事务的自动管理,使得开发者无需在业务代码中显式调用事务开始、提交或回滚等操作。下面我们将详细探讨Spring 2.x的声明...
Spring 提供了多种方式来实现声明式事务管理,例如使用 @Transactional 注解或使用 XML 配置文件。 总结 本文通过详细分析 Spring 的编程式事务管理及声明式事务管理,帮助读者理解事务管理的重要性和实现方式。...
通过这个Demo,开发者可以学习如何在Spring与iBatis集成的环境中配置和使用声明式事务,理解事务的ACID属性(原子性、一致性、隔离性和持久性)如何在实际项目中得到保障。此外,这也是理解和实践Spring AOP以及依赖...
以下是对Spring声明式事务配置的详细说明: 1. **Spring 1.x 声明事务方式** 在Spring 1.x中,声明式事务有两种主要的配置方式,第一种是通过XML配置逐个为每个业务类创建事务代理。首先,你需要声明一个事务管理...