`

Spring的四种声明式事务的配置-Hibernate事务

阅读更多

以下两个bean的配置是下面要用到的。


  
Xml代码
  1. <!-- 定义事务管理器(声明式的事务) -->  
  2.  <bean id="transactionManager"  
  3.   class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  4.   <property name="sessionFactory">  
  5.    <ref local="sessionFactory" />  
  6.   </property>  
  7.  </bean>  
  8.   
  9. <!-- 业务逻辑层(是对各个DAO层的正面封装)主要用到<<门面模式>> -->  
  10.  <bean id="fundService"  
  11.   class="com.jack.fund.service.serviceimpl.FundService">  
  12.   <property name="operdao">  
  13.    <ref bean="operatorDAO" />  
  14.   </property>  
  15.   <property name="producedao">  
  16.    <ref bean="fundProduceDAO" />  
  17.   </property>  
  18.   <property name="customerdao">  
  19.    <ref bean="customerDAO" />  
  20.   </property>  
  21.   <property name="accountdao">  
  22.    <ref bean="accountDAO" />  
  23.   </property>  
  24.   <property name="fundaccountdao">  
  25.    <ref bean="fundAccountDAO" />  
  26.   </property>  
  27.   <property name="fundtransdao">  
  28.    <ref bean="fundTransDAO" />  
  29.   </property>  
  30.  </bean>  
      可能还有其他很多模块。<bean id="fundService"/>可能只是其中的模块。

第一种:配置声明式事务的方法如下。也是我们最常用的方法了,它适用于你的库表比较少的情况下。

Xml代码
  1. <bean id="fundServiceDAOProxy"  
  2.   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  3.   <!-- 配置事务管理器 -->  
  4.   <property name="transactionManager">  
  5.    <ref bean="transactionManager" />  
  6.   </property>  
  7.   <!-- 此属性指定目标类本省是否是代理的对象.如果目标类没有实现任何类,就设为true代表自己 -->  
  8.   <property name="proxyTargetClass">  
  9.    <value>false</value>  
  10.   </property>  
  11.   <property name="proxyInterfaces">  
  12.    <value>com.jack.fund.service.IFundService</value>  
  13.   </property>  
  14.   <!-- 目标bean -->  
  15.   <property name="target">  
  16.    <ref bean="fundService" />  
  17.   </property>  
  18.   <!-- 配置事务属性 -->  
  19.   <property name="transactionAttributes">  
  20.    <props>  
  21.     <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  22.     <prop key="add*">PROPAGATION_REQUIRED</prop>  
  23.     <prop key="update*">PROPAGATION_REQUIRED</prop>  
  24.     <prop key="save*">PROPAGATION_REQUIRED</prop>  
  25.     <prop   key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
  26.    </props>  
  27.   </property>  
  28.  </bean>  


     以下可能还有其他的xxxServiceDAOProxy.大家可以看出针对每一个功能模块配置一个业务代理服务。如果模块多大话,就显得代码有点多了,发现他们只是稍微一点不一样。这时我们就应该想到继承的思想。用第二种方法。

第二种:配置声明式事务的方法如下。这种情况适合相对比较多的模块时使用。

Xml代码
  1. <!-- 利用继承的思想简化配置,要把abstract="true" -->  
  2.  <bean id="transactionBase"  
  3.   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
  4.   lazy-init="true" abstract="true">  
  5.   <!-- 配置事务管理器 -->  
  6.   <property name="transactionManager">  
  7.    <ref bean="transactionManager" />  
  8.   </property>  
  9.   <!-- 配置事务属性 -->  
  10.   <property name="transactionAttributes">  
  11.    <props>  
  12.     <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  13.     <prop key="add*">PROPAGATION_REQUIRED</prop>  
  14.     <prop key="update*">PROPAGATION_REQUIRED</prop>  
  15.     <prop key="save*">PROPAGATION_REQUIRED</prop>  
  16.     <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
  17.    </props>  
  18.   </property>  
  19.  </bean>  


    而具体的模块可以简单的这样配置。
    只要指明它的parent(父类)就可以了。
    父类一般把abstract="true",因为在容器加载的时候不需要初始化,等到用的时候再有它的子类调用的时候,再去初始化。

Xml代码
  1. <bean id="fundServiceDAOProxy" parent="transactionBase" >  
  2.   <property name="target">  
  3.   <ref bean="fundService" />  
  4.   </property>  
  5.  </bean>  


    这样配置的话,如果有多个像fundService这样模块时,可以少些很多重复的代码。

第三种:配置声明式事务的方法如下。主要利用BeanNameAutoProxyCreator自动创建事务代理


Xml代码
  1.  <bean id="transactionInterceptor"  
  2.  class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  3.  <property name="transactionManager">  
  4.   <ref bean="transactionManager" />  
  5.  </property>  
  6.  <!-- 配置事务属性 -->  
  7.  <property name="transactionAttributes">  
  8.   <props>  
  9.    <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  10.    <prop key="add*">PROPAGATION_REQUIRED</prop>  
  11.    <prop key="update*">PROPAGATION_REQUIRED</prop>  
  12.    <prop key="save*">PROPAGATION_REQUIRED</prop>  
  13.    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
  14.   </props>  
  15.  </property>  
  16. </bean>  
  17.   
  18. <bean  
  19.  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  20.  <property name="beanNames">  
  21.   <list>  
  22.    <value>fundService</value>  
  23.   </list>  
  24.  </property>  
  25.  <property name="interceptorNames">  
  26.   <list>  
  27.    <value>transactionInterceptor</value>  
  28.   </list>  
  29.  </property>  
  30. </bean>


这种方法主要利用了拦截器的原理。

    前三种方法一般都必需指定具体的模块bean.
    如果模块过多话,比如一个大型的网站一般有几十个模块,我们就得考虑用第四种的配置方式了。自动创建事务代理的方式了。

第四种:配置声明式事务的方法如下。


Xml代码
  1. <bean id="transactionInterceptor"  
  2.   class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  3.   <property name="transactionManager">  
  4.    <ref bean="transactionManager" />  
  5.   </property>  
  6. <property name="transactionAttributes">
       <props>
        <prop key="*">PROPAGATION_REQUIRED</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
      </property>
  7. </bean>  
  8.   
  9. <!-- 自动代理 -->  
  10.  <bean id="autoproxy"  
  11.   class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  12.   <!-- 可以是Service或Dao或Action(最好是针对业务层*Service) -->  
  13.   <property name="beanNames">  
  14.    <list>  
  15.     <value>*Service,*Action,*Dao</value>  
  16.    </list>  
  17.   </property>  
  18.   <property name="interceptorNames">  
  19.    <list>  
  20.        <value>transactionInterceptor</value>  
  21.    </list>  
  22.   </property>  
  23.  </bean>  

    自动代理还有一种用法就是结合正则表达式和advice使用。


Xml代码
  1. <bean id="transactionInterceptor"  
  2.   class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  3.   <property name="transactionManager">  
  4.    <ref bean="transactionManager" />  
  5.   </property>  
  6. </bean>  
  7.   
  8.  <bean id="autoProxyCreator"  
  9.   class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >  
  10.  </bean>  
  11.   
  12. <bean id="regexpMethodPointcutAdvisor"  
  13.   class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  14.   <property name="advice">  
  15.   <ref bean="transactionInterceptor" />  
  16.   </property>  
  17.   <property name="pattern">  
  18.   <value>.*</value>  
  19.   </property>  
  20.  </bean>  

      这个方法可以针对具体的模块进行拦截并进行事务处理。

    在你的实际项目中,你可以根据你的情况选用不同的方法。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics