Spring的四種聲明式事務的配置-Hibernate事務

Spring的四種聲明式事務的配置-Hibernate事務
 

如下兩個bean的配置是下面要用到的。正則表達式

    <!-- 定義事務管理器(聲明式的事務) -->
     <bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
       <ref local="sessionFactory" />
      </property>
     </bean>
spring

    <!-- 業務邏輯層(是對各個DAO層的正面封裝)主要用到<<門面模式>> -->
     <bean id="fundService"
      class="com.jack.fund.service.serviceimpl.FundService">
      <property name="operdao">
       <ref bean="operatorDAO" />
      </property>
      <property name="producedao">
       <ref bean="fundProduceDAO" />
      </property>
      <property name="customerdao">
       <ref bean="customerDAO" />
      </property>
      <property name="accountdao">
       <ref bean="accountDAO" />
      </property>
      <property name="fundaccountdao">
       <ref bean="fundAccountDAO" />
      </property>
      <property name="fundtransdao">
       <ref bean="fundTransDAO" />
      </property>
     </bean>
session

    可能還有其餘不少模塊。<bean id="fundService"/>可能只是其中的模塊。網站

第一種:配置聲明式事務的方法以下。也是咱們最經常使用的方法了,它適用於你的庫表比較少的狀況下。spa

    <bean id="fundServiceDAOProxy"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <!-- 配置事務管理器 -->
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
      <!-- 此屬性指定目標類本省是不是代理的對象.若是目標類沒有實現任何類,就設爲true表明本身 -->
      <property name="proxyTargetClass">
       <value>false</value>
      </property>
      <property name="proxyInterfaces">
       <value>com.jack.fund.service.IFundService</value>
      </property>
      <!-- 目標bean -->
      <property name="target">
       <ref bean="fundService" />
      </property>
      <!-- 配置事務屬性 -->
      <property name="transactionAttributes">
       <props>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="add*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop   key="find*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
      </property>
     </bean>
     如下可能還有其餘的xxxServiceDAOProxy.你們能夠看出針對每個功能模塊配置一個業務代理服務。若是模塊多大話,就顯得代碼有點多了,發現他們只是稍微一點不同。這時咱們就應該想到繼承的思想。用第二種方法。
hibernate

第二種:配置聲明式事務的方法以下。這種狀況適合相對比較多的模塊時使用。代理

    <!-- 利用繼承的思想簡化配置,要把abstract="true" -->
     <bean id="transactionBase"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
      lazy-init="true" abstract="true">
      <!-- 配置事務管理器 -->
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
      <!-- 配置事務屬性 -->
      <property name="transactionAttributes">
       <props>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="add*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
      </property>
     </bean>
    而具體的模塊能夠簡單的這樣配置。
    只要指明它的parent(父類)就能夠了。
    父類通常把abstract="true",由於在容器加載的時候不須要初始化,等到用的時候再有它的子類調用的時候,再去初始化。
    <bean id="fundServiceDAOProxy" parent="transactionBase" >
      <property name="target">
      <ref bean="fundService" />
      </property>
     </bean>
    這樣配置的話,若是有多個像fundService這樣模塊時,能夠少些不少重複的代碼。
regexp

第三種:配置聲明式事務的方法以下。主要利用BeanNameAutoProxyCreator自動建立事務代理orm

      <bean id="transactionInterceptor"
      class="org.springframework.transaction.interceptor.TransactionInterceptor">
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
      <!-- 配置事務屬性 -->
      <property name="transactionAttributes">
       <props>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="add*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
      </property>
     </bean>
對象

     <bean
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="beanNames">
       <list>
        <value>fundService</value>
       </list>
      </property>
      <property name="interceptorNames">
       <list>
        <value>transactionInterceptor</value>
       </list>
      </property>
     </bean>
    這種方法主要利用了攔截器的原理。

    前三種方法通常都必需指定具體的模塊bean.
    若是模塊過多話,好比一個大型的網站通常有幾十個模塊,咱們就得考慮用第四種的配置方式了。自動建立事務代理的方式了。

第四種:配置聲明式事務的方法以下。

    <bean id="transactionInterceptor"
      class="org.springframework.transaction.interceptor.TransactionInterceptor">
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
    </bean>

    <!-- 自動代理 -->
     <bean id="autoproxy"
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <!-- 能夠是Service或DAO層(最好是針對業務層*Service) -->
      <property name="beanNames">
       <list>
        <value>*Service</value>
       </list>
      </property>
      <property name="interceptorNames">
       <list>
           <value>transactionInterceptor</value>
       </list>
      </property>
     </bean>

    自動代理還有一種用法就是結合正則表達式和advice使用。

    <bean id="transactionInterceptor"
      class="org.springframework.transaction.interceptor.TransactionInterceptor">
      <property name="transactionManager">
       <ref bean="transactionManager" />
      </property>
    </bean>

     <bean id="autoProxyCreator"
      class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >
     </bean>

    <bean id="regexpMethodPointcutAdvisor"
      class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="advice">
      <ref bean="transactionInterceptor" />
      </property>
      <property name="pattern">
      <value>.*</value>
      </property>
     </bean>

      這個方法能夠針對具體的模塊進行攔截並進行事務處理。

    在你的實際項目中,你能夠根據你的狀況選用不一樣的方法。

相關文章
相關標籤/搜索