明月松間照 清泉石上流


                                                  ——— 兵臨城下   貓科動物
          posts - 70, comments - 137, trackbacks - 0, articles - 23
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Spring 學習筆記(一)

          Posted on 2006-12-04 11:57 兵臨城下 閱讀(348) 評論(0)  編輯  收藏 所屬分類: Spring
          聲明式的事務劃分

          我們可以使用Spring的AOP TransactionInterceptor來替換事務劃分的手工代碼, 這需要在application context中定義interceptor。 這個方案使得你可以把業務對象從每個業務方法中重復的事務劃分代碼中解放出來。 此外,像傳播行為和隔離級別等事務概念能夠在配置文件中改變,而不會影響業務對象的實現。

          <beans>
          
              ...
          
              <bean id="myTransactionManager" 
                  class="org.springframework.orm.hibernate.HibernateTransactionManager">
                  <property name="sessionFactory">
                      <ref bean="mySessionFactory"/>
                  </property>
              </bean>
          
              <bean id="myTransactionInterceptor" 
                  class="org.springframework.transaction.interceptor.TransactionInterceptor">
                  <property name="transactionManager">
                      <ref bean="myTransactionManager"/>
                  </property>
                  <property name="transactionAttributeSource">
                      <value>
                          product.ProductService.increasePrice*=PROPAGATION_REQUIRED
                          product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY
                      </value>
                  </property>
              </bean>
          
              <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
                  <property name="productDao">
                      <ref bean="myProductDao"/>
                  </property>
              </bean>
          
              <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean">
                  <property name="proxyInterfaces">
                      <value>product.ProductService</value>
                  </property>
                  <property name="interceptorNames">
                      <list>
                          <value>myTransactionInterceptor</value>
                          <value>myProductServiceTarget</value>
                      </list>
                  </property>
              </bean>
          
          </beans>
          public class ProductServiceImpl implements ProductService {
          
              private ProductDao productDao;
          
              public void setProductDao(ProductDao productDao) {
                  this.productDao = productDao;
              }
          
              public void increasePriceOfAllProductsInCategory(final String category) {
                  List productsToChange = this.productDAO.loadProductsByCategory(category);
                  ...
              }
          
              ...
          
          }







          一個簡便可選的創建聲明式事務的方法是:TransactionProxyFactoryBean, 特別是在沒有其他AOP interceptor牽扯到的情況下。對一個特定的目標bean,
          TransactionProxyFactoryBean用事務配置自己聯合proxy定義。 這樣就把配置工作減少為配置一個目標bean以及一個 proxy bean的定義
          (少了interceptor的定義)。 此外你也不需要指定事務方法定義在哪一個接口或類中。
          <beans>
          
              ...
          
              <bean id="myTransactionManager" 
                  class="org.springframework.orm.hibernate.HibernateTransactionManager">
                  <property name="sessionFactory">
                      <ref bean="mySessionFactory"/>
                  </property>
              </bean>
          
              <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
                  <property name="productDao">
                      <ref bean="myProductDao"/>
                  </property>
              </bean>
          
              <bean id="myProductService" 
                  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                  <property name="transactionManager">
                      <ref bean="myTransactionManager"/>
                  </property>
                  <property name="target">
                      <ref bean="myProductServiceTarget"/>
                  </property>
                  <property name="transactionAttributes">
                      <props>
                          <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
                          <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop>
                      </props>
                  </property>
              </bean>
          
          </beans>
          主站蜘蛛池模板: 象州县| 临漳县| 丽水市| 山西省| 鹤山市| 天镇县| 长兴县| 顺昌县| 仁化县| 株洲县| 丹江口市| 光山县| 成都市| 石台县| 原平市| 宣汉县| 博罗县| 漳平市| 台中县| 自贡市| 绥棱县| 福贡县| 若尔盖县| 郯城县| 五台县| 登封市| 晴隆县| 河北区| 满洲里市| 鹤峰县| 黎平县| 台中市| 连城县| 濮阳县| 开平市| 东乡族自治县| 丽江市| 林芝县| 罗平县| 琼海市| 德化县|