少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          一.
          使用TransactionProxyFactoryBean創(chuàng)建事務(wù)代理(通常事務(wù)代理以Service層為目標(biāo)bean)
          <bean id="personService" class="com.lin.personServiceImpl">
              <property name="personDao" ref="personDao"/>
          </bean>
          //配置hibernate的事務(wù)管理器,使用HibernateTransactionManager類,該類實(shí)現(xiàn)了PlatformTransactionManager接口,針對hibernate 持久化連接的特定實(shí)現(xiàn)
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
          </bean>
          //配置personService bean的事務(wù)代理
          <bean id="personServiceProxy"
                  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                      //指定事務(wù)管理器
              <property name="transactionManager" ref="transactionManager"/>
                      //指定需要生成代理的日標(biāo)bean
              <property name="persionService" ref="persionService"/>
                      //指定事務(wù)屬性
              <property name="transactionAttributes"
                  <props>
                      <prop key="insert*">PROPAGATION_REQUIRED,-MyCheckedException</prop>
                      <prop key="update*>PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                  </props>
              </property>


          二.使用自動創(chuàng)建代理簡化事務(wù)配置
             使用BeanNameAutoProxyCreator 和DefaultAdvisorAutoProxyCreator創(chuàng)建代理時,并不一定是創(chuàng)建事務(wù)代理,關(guān)鍵在于傳入的攔截器,如果傳入事務(wù)攔截器,將可自動生成事務(wù)代理.
          //使用jdbc局部事務(wù)策略
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"/>
          </bean>
          //配置目標(biāo)bean1,該目標(biāo)bean將由Bean后處理器自動生成代理
          <bean id="testbean1" class="com.lin.Testbean1Impl">
              <property name="dataSource" ref="dataSource"/>
          </bean
          //配置目標(biāo)bean2,該目標(biāo)bean將由Bean后處理器自動生成代理
          <bean id="testbean2" class="com.lin.Testbean2Impl">
              <property name="dataSource" ref="dataSource"/>
          </bean
          //配置事務(wù)攔截器bean
          <bean id="transactionInterceptor"
             class="org.springframework.transaction.interceptor.TransactionInterceptor">
                  //事務(wù)攔截器需要注入一個事務(wù)管理器
                <property name="transactionManager" ref="transactionManager"/>
                 <property name="transactionAttributes">
                      //定義事務(wù)傳播屬性
                      <props>
                              <prop key="insert*">PROPAGATION_REQUIRED</prop>
                              <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                              <prop key="*">PROPAGATION_REQUIRED</prop>
                      </props>
                  </property>
              //定義BeanNameAutoProxyCreator的Bean后處理器
          <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
              <property name="beanNames">
                  <list>
                      <value>testbean1</value>
                      <value>testbean2</value>
                  </list>
                      //此處可以增加其他需要創(chuàng)建事務(wù)代理的bean
              </property>
                  //定義BeanNameAutoProxyCreator所需要的攔截器
               <property name="interceptorNames">
                  <list>
                      <value>transactionInterceptor</value>
                          //此處可以增加其他新的Interceptor
                  </list>
              </property>
          </bean>
          posted on 2012-03-04 16:37 abin 閱讀(369) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 西乌珠穆沁旗| 嵊泗县| 丽江市| 菏泽市| 永宁县| 开鲁县| 永州市| 丰宁| 通州市| 西林县| 交城县| 莲花县| 来宾市| 大新县| 长寿区| 雅安市| 宁陕县| 普宁市| 辰溪县| 大庆市| 郓城县| 东至县| 右玉县| 报价| 措勤县| 怀集县| 宿迁市| 黔江区| 敦化市| 武山县| 临江市| 丹寨县| 温宿县| 界首市| 县级市| 桐梓县| 类乌齐县| 寿阳县| 鹿泉市| 齐齐哈尔市| 龙陵县|