熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

對Spring事務配置方式的深入研究

2022-06-13   來源: Java開源技術 

  前段時間對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

  public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

  public List listUsers() {

  return thisgetSession()createQuery(from Userlist()

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28876.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.