隨筆 - 37  文章 - 14  trackbacks - 0
          <2007年8月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          相關鏈接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          二、聲明式事務管理
                  Spring聲明式管理主要是通過TransactionProxyFactoryBean來包裝要管理的目標對象,代理目標對象要實現的接口。
          看如下代碼
          首先定義一個目標對象

              <bean id="spcJlxDataSaveServiceTarget"
                  class
          ="com.spc.spring.business.service.SpcJlxDataSaveServiceImp"
                  abstract
          ="false" singleton="true" lazy-init="default"
                  autowire
          ="default" dependency-check="default">
                  
          <property name="spcGzxmServiceImp">
                      
          <ref bean="spcGzxmServiceImp" />
                  
          </property>
                  
          <property name="spcJlxDataService">
                      
          <ref bean="spcJlxDataService" />
                  
          </property>
              
          </bean>

          同編程式事務管理,都需要有transactionManager的對象,這里就不再重復設置;下面看如果設置TransactionProxyFactoryBean來代理目標對象

               <bean id="spcJlxDataSaveServiceImp"
               class
          ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
               
          <property name="proxyInterfaces">
                   
          <list>
                       
          <value>com.spc.spring.business.ISpcJlxDataSaveService</value>
                   
          </list>
               
          </property>
               
          <property name="target">
                   
          <ref bean="spcJlxDataSaveServiceTarget"/>
               
          </property>
               
          <property name="transactionManager">
                   
          <ref bean="transactionManager"/>
               
          </property>
               
          <property name="transactionAttributes">
                   
          <props>
                   
          <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                   
          </props>
               
          </property>
               
          </bean>

          target屬性是目標對象;proxyInterface是目標對象所實現的接口;transactionAttributes屬性是定義事務的設置級別等。其中"-Exception"表示對所有的異常都做回滾動作。
          TransactionTemplatate
          如果要給對象添加更多的事務屬性,那么使用上述配置便會變得非常臃腫。為了避免該問題你可以使用Transaction Template Beanr為了繼承這個bean
          的所有bean指定屬性如下,1:先定義一個tmplate bean

          <bean id="txProxyTemplate" abstract="true"
          class
          ="org.springframework.transaction.interceptor.
          TransactionProxyFactoryBean"
          >
          <property name="transactionManager">
          <ref bean="transactionManager"/>
          </property>
          <property name="transactionAttributes">
          <props>
          <prop key="save*">PROPAGATION_REQUIRED</prop>
          <prop key="remove*">PROPAGATION_REQUIRED</prop>
          <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
          </property>
          </bean>
          2:創建一個應用此模板的bean時,使用parent屬性來引用此模板的id。然后在target屬性中將這個類定義為一個內部bean
          <bean id="userManager" parent="txProxyTemplate">
          <property name="target">
          <bean class="org.appfuse.service.impl.UserManagerImpl">
          <property name="userDAO"><ref bean="userDAO"/></property>
          </bean>
          </property>
          </bean>
          TransactionAttributeSource
          另一種配置聲明式事務的是指定一個引用了NameMatchTransactionAttributeSource類并定義了方法和它們行為的bean。這種策略不及template bean簡潔,因為它要求用TransactionProxyFactoryBean來包裝所有的bean。
          1:創建一個bean來描述事務屬性
          <bean name="txAttributes"
          class
          ="org.springframework.transaction.interceptor.
          NameMatchTransactionAttributeSource"
          >
          <property name="properties">
          <props>
          <prop key="save*">PROPAGATION_REQUIRED</prop>
          <prop key="remove*">PROPAGATION_REQUIRED</prop>
          <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
          </property>
          </bean>

          2:創建一個包裝事務的bean定義,在transactionAttributeSource屬性中引用這個bean

          <bean id="userManager"
          class
          ="org.springframework.transaction.interceptor.
          TransactionProxyFactoryBean"
          >
          <property name="transactionManager">
          <ref bean="transactionManager"/>
          </property>
          <property name="target">
          <bean class="org.appfuse.service.impl.UserManagerImpl">
          <property name="userDAO">
          <ref bean="userDAO"/>
          </property>
          </bean>
          </property>
          <property name="transactionAttributeSource">
          <ref bean="txAttributes"/>
          </property>
          </bean>
          posted on 2007-08-09 14:46 扭曲的鉛筆 閱讀(253) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 孙吴县| 贵溪市| 昆明市| 梓潼县| 邹平县| 田阳县| 黄冈市| 绵竹市| 黑水县| 保靖县| 永康市| 朝阳市| 科技| 凤冈县| 拜泉县| 清水河县| 平山县| 宜丰县| 卢龙县| 临湘市| 临潭县| 松阳县| 拜城县| 宜黄县| 长寿区| 宁强县| 肇东市| 罗源县| 垦利县| 潮州市| 丰顺县| 凤冈县| 宣汉县| 遵义县| 西吉县| 铜梁县| 鲁甸县| 高安市| 图们市| 阿拉善盟| 娱乐|