前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的。
总结如下:
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。
具体如下图:
根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:
第一种方式:每个Bean都有一个代理
第二种方式:所有Bean共享一个代理基类
第三种方式:使用拦截器
第四种方式:使用tx标签配置的拦截器
第五种方式:全注解
此时在DAO上需加上@Transactional注解,如下:
根据代理机制不同,有以下几种配置方式:
先定义一个DAO接口及实现类:
Java代码
public interface IUserDao {
public void insertUser(UserTable user);
}
public interface IUserDao {
public void insertUser(UserTable user);
}
实现类:
Java代码
public class UserDaoImpl extendsHibernateDaoSupport implements IUserDao{
public void insertUser(UserTable user) {
getHibernateTemplate().saveOrUpdate(user);
}
}
public class UserDaoImpl extendsHibernateDaoSupport implements IUserDao{
publicvoid insertUser(UserTable user) {
getHibernateTemplate().saveOrUpdate(user);
}
}
第一种
为每个bean设置一个代理,这种是根据具体需求来定,如要对具体到每个交易进行事务操作的话,这个方式是最合适的;
Java代码
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 每个bean都有一个代理 -->
<beans>
<!-- sessionFactory相当于spring datasource -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction2.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 每个bean都有一个代理<property name="target" ref="userDaoTarget" /> -->
<bean id="userDaoProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事务管理器 -->
<propertyname="transactionManager" ref="transactionManager" />
<property name="target"ref="userDao" />
<propertyname="proxyInterfaces" value="org.lgh.spring.transaction2.IUserDao"/>
<!-- 配置事务属性 -->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 每个bean都有一个代理 -->
<beans>
<!--sessionFactory相当于springdatasource -->
<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="userDao"class="org.lgh.spring.transaction2.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 每个bean都有一个代理 <propertyname="target" ref="userDaoTarget" /> -->
<bean id="userDaoProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事务管理器-->
<property name="transactionManager"ref="transactionManager" />
<property name="target" ref="userDao" />
<property name="proxyInterfaces"value="org.lgh.spring.transaction2.IUserDao" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
第二种:
所有的bean共享一个代理
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 所有的bean共享一个代理-->
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一个代理/>-->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<propertyname="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<propertyname="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction3.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一个代理/>-->
<bean id="userDaoProxy"parent="transactionBase" >
<property name="target"ref="userDao" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 所有的bean共享一个代理-->
<beans>
<beanid="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共享一个代理/> -->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器-->
<property name="transactionManager"ref="transactionManager" />
<!-- 配置事务属性-->
<property name="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction3.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 所有的bean共享一个代理/> -->
<bean id="userDaoProxy" parent="transactionBase">
<property name="target" ref="userDao" />
</bean>
</beans>
第三种:
使用拦截器 来配置你的事务,这个主要是进行一些方法调用前后进行一些其他事件的处理,如进行权限检查等...
代码如下:
Java代码
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 使用拦截器 -->
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<propertyname="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<propertyname="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="logger"class="org.lgh.spring.transaction4.SpringAOPInterceptor">
</bean>
<bean id="logBeforeAdvice"class="com.spring.advices.LogBeforeAdvice">
</bean>
<!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理 -->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 加上此句就不会抛出 $Proxy cannot be cast的异常啦-->
<propertyname="proxyTargetClass">
<value>true</value>
</property>
<property name="beanNames">
<list>
<!-- *Dao对应下面的userDao要对它进行拦截-->
<value>userDao</value>
</list>
</property>
<propertyname="interceptorNames">
<list>
<value>transactionInterceptor</value>
<value>logBeforeAdvice</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction4.UserDaoImpl">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 服务层 service-->
<bean id="userService"class="org.lgh.spring.transaction4.UserServiceImpl">
<property name="userDao"ref="userDao" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 使用拦截器 -->
<beans>
<beanid="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>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="logger"class="org.lgh.spring.transaction4.SpringAOPInterceptor">
</bean>
<bean id="logBeforeAdvice"class="com.spring.advices.LogBeforeAdvice">
</bean>
<!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理 -->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 加上此句就不会抛出$Proxy cannot be cast的异常啦-->
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="beanNames">
<list>
<!-- *Dao对应下面的userDao要对它进行拦截-->
<value>userDao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
<value>logBeforeAdvice</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction4.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
<!-- 服务层service-->
<bean id="userService"class="org.lgh.spring.transaction4.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
</beans>
第四种:
使用aop:config配置方式
Java代码
<?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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="org.lgh.spring.transaction5"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:configproxy-target-class="true" >
<aop:pointcutid="interceptorPointCuts"
expression="execution(*org.lgh.spring.transaction5.*.*(..))" />
<aop:advisoradvice-ref="txAdvice"
pointcut-ref="interceptorPointCuts"/>
</aop:config>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction5.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scanbase-package="org.lgh.spring.transaction5" />
<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 proxy-target-class="true" >
<aop:pointcutid="interceptorPointCuts"
expression="execution(* org.lgh.spring.transaction5.*.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction5.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
</beans>
还有一种就是采用全注解方式:
代码如下:
Java代码
<?xml version="1.0"encoding="UTF-8"?>
<!--全注解 -->
<beansxmlns="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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scanbase-package="org.lgh.spring.transaction6" />
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation" value="classpath:hibernate.cfg.xml"/>
<propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction6.UserDaoImpl">
<property name="sessionFactory"ref="sessionFactory" />
</bean>
</beans>
<?xml version="1.0"encoding="UTF-8"?>
<!--全注解 -->
<beansxmlns="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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scanbase-package="org.lgh.spring.transaction6" />
<tx:annotation-driven proxy-target-class ="true"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>
<!-- 配置DAO -->
<bean id="userDao"class="org.lgh.spring.transaction6.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>