java

位置:IT落伍者 >> java >> 浏览文章

Spring事务配置的五种方式


发布日期:2022年01月12日
 
Spring事务配置的五种方式

前段时间对Spring的事务配置做了比较深入的研究在此之间对Spring的事务配置虽说也配置过但是一直没有一个清楚的认识通过这次的学习发觉Spring的事务配置只要把思路理清还是比较好掌握的

总结如下

Spring配置文件中关于事务配置总是由三个组成部分分别是DataSourceTransactionManager和代理机制这三部分无论哪种配置方式一般变化的只是代理机制这部分

DataSourceTransactionManager这两部分只是会根据数据访问方式有所变化比如使用Hibernate进行数据访问时DataSource实际为SessionFactoryTransactionManager的实现为HibernateTransactionManager

具体如下图

根据代理机制的不同总结了五种Spring事务的配置方式配置文件如下

第一种方式每个Bean都有一个代理

<?xml version= encoding=UTF?>

<beans xmlns=

xmlns:xsi=instance

xmlns:context=

xmlns:aop=

xsi:schemaLocation=

beansxsd

contextxsd

aopxsd>

<bean id=sessionFactory

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<property name=configLocation value=classpath:hibernatecfgxml/>

<property name=configurationClass value=orghibernatecfgAnnotationConfiguration/>

</bean>

<! 定义事务管理器(声明式的事务) >

<bean id=transactionManager

class=orgspringframeworkormhibernateHibernateTransactionManager>

<property name=sessionFactory ref=sessionFactory/>

</bean>

<! 配置DAO >

<bean id=userDaoTarget class=comblueskyspringdaoUserDaoImpl>

<property name=sessionFactory ref=sessionFactory/>

</bean>

<bean id=userDao

class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>

<! 配置事务管理器 >

<property name=transactionManager ref=transactionManager/>

<property name=target ref=userDaoTarget/>

<property name=proxyInterfaces value=comblueskyspringdaoGeneratorDao/>

<! 配置事务属性 >

<property name=transactionAttributes>

<props>

<prop key=*>PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>

</beans>

第二种方式所有Bean共享一个代理基类

<?xml version= encoding=UTF?>

<beans xmlns=

xmlns:xsi=instance

xmlns:context=

xmlns:aop=

xsi:schemaLocation=

beansxsd

contextxsd

aopxsd>

<bean id=sessionFactory

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<property name=configLocation value=classpath:hibernatecfgxml/>

<property name=configurationClass value=orghibernatecfgAnnotationConfiguration/>

</bean>

<! 定义事务管理器(声明式的事务) >

<bean id=transactionManager

class=orgspringframeworkormhibernateHibernateTransactionManager>

<property name=sessionFactory ref=sessionFactory/>

</bean>

<bean id=transactionBase

class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean

lazyinit=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=comblueskyspringdaoUserDaoImpl>

<property name=sessionFactory ref=sessionFactory/>

</bean>

<bean id=userDao parent=transactionBase>

<property name=target ref=userDaoTarget/>

</bean>

</beans>

第三种方式使用拦截器

<?xml version= encoding=UTF?>

<beans xmlns=

xmlns:xsi=instance

xmlns:context=

xmlns:aop=

xsi:schemaLocation=

beansxsd

contextxsd

aopxsd>

<bean id=sessionFactory

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<property name=configLocation value=classpath:hibernatecfgxml/>

<property name=configurationClass value=orghibernatecfgAnnotationConfiguration/>

</bean>

<! 定义事务管理器(声明式的事务) >

<bean id=transactionManager

class=orgspringframeworkormhibernateHibernateTransactionManager>

<property name=sessionFactory ref=sessionFactory/>

</bean>

<bean id=transactionInterceptor

class=orgspringframeworktransactioninterceptorTransactionInterceptor>

<property name=transactionManager ref=transactionManager/>

<! 配置事务属性 >

<property name=transactionAttributes>

<props>

<prop key=*>PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>

<bean class=orgspringframeworkaopframeworkautoproxyBeanNameAutoProxyCreator>

<property name=beanNames>

<list>

<value>*Dao</value>

</list>

</property>

<property name=interceptorNames>

<list>

<value>transactionInterceptor</value>

</list>

</property>

</bean>

<! 配置DAO >

<bean id=userDao class=comblueskyspringdaoUserDaoImpl>

<property name=sessionFactory ref=sessionFactory/>

</bean>

</beans>

第四种方式使用tx标签配置的拦截器

<?xml version= encoding=UTF?>

<beans xmlns=

xmlns:xsi=instance

xmlns:context=

xmlns:aop=

xmlns:tx=

xsi:schemaLocation=

beansxsd

contextxsd

aopxsd

txxsd>

<context:annotationconfig />

<context:componentscan basepackage=combluesky/>

<bean id=sessionFactory

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<property name=configLocation value=classpath:hibernatecfgxml/>

<property name=configurationClass value=orghibernatecfgAnnotationConfiguration/>

</bean>

<! 定义事务管理器(声明式的事务) >

<bean id=transactionManager

class=orgspringframeworkormhibernateHibernateTransactionManager>

<property name=sessionFactory ref=sessionFactory/>

</bean>

<tx:advice id=txAdvice transactionmanager=transactionManager>

<tx:attributes>

<tx:method name=* propagation=REQUIRED/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut id=interceptorPointCuts

expression=execution(* comblueskyspringdao**())/>

<aop:advisor adviceref=txAdvice

pointcutref=interceptorPointCuts/>

</aop:config>

</beans>

第五种方式全注解

<?xml version= encoding=UTF?>

<beans xmlns=

xmlns:xsi=instance

xmlns:context=

xmlns:aop=

xmlns:tx=

xsi:schemaLocation=

beansxsd

contextxsd

aopxsd

txxsd>

<context:annotationconfig />

<context:componentscan basepackage=combluesky/>

<tx:annotationdriven transactionmanager=transactionManager/>

<bean id=sessionFactory

class=orgspringframeworkormhibernateLocalSessionFactoryBean>

<property name=configLocation value=classpath:hibernatecfgxml/>

<property name=configurationClass value=orghibernatecfgAnnotationConfiguration/>

</bean>

<! 定义事务管理器(声明式的事务) >

<bean id=transactionManager

class=orgspringframeworkormhibernateHibernateTransactionManager>

<property name=sessionFactory ref=sessionFactory/>

</bean>

</beans>

此时在DAO上需加上@Transactional注解如下

package comblueskyspringdao;

import javautilList;

import orghibernateSessionFactory;

import orgspringframeworkbeansfactoryannotationAutowired;

import orgspringframeworkormhibernatesupportHibernateDaoSupport;

import orgspringframeworkstereotypeComponent;

import comblueskyspringdomainUser;

@Transactional

@Component(userDao)

publicclass UserDaoImpl extends HibernateDaoSupport implements UserDao {

public List<User> listUsers() {

returnthisgetSession()createQuery(from User)list();

}

}

上一篇:Spring MVC学习之处理方法返回值的可选类型

下一篇:开源技术:Eclipse 3.2冒险之旅