少年阿賓

          那些青春的歲月

            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接口,針對(duì)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>


          二.使用自動(dòng)創(chuàng)建代理簡(jiǎn)化事務(wù)配置
             使用BeanNameAutoProxyCreator 和DefaultAdvisorAutoProxyCreator創(chuàng)建代理時(shí),并不一定是創(chuàng)建事務(wù)代理,關(guān)鍵在于傳入的攔截器,如果傳入事務(wù)攔截器,將可自動(dòng)生成事務(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后處理器自動(dòng)生成代理
          <bean id="testbean1" class="com.lin.Testbean1Impl">
              <property name="dataSource" ref="dataSource"/>
          </bean
          //配置目標(biāo)bean2,該目標(biāo)bean將由Bean后處理器自動(dòng)生成代理
          <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ù)攔截器需要注入一個(gè)事務(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>
                      //此處可以增加其他需要?jiǎng)?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) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 洛阳市| 富川| 黑河市| 汤原县| 平凉市| 六枝特区| 湘西| 兰溪市| 武威市| 时尚| 泌阳县| 攀枝花市| 维西| 庆安县| 五常市| 嵊州市| 馆陶县| 鄂伦春自治旗| 五大连池市| 华安县| 包头市| 静宁县| 宜城市| SHOW| 安溪县| 布尔津县| 永平县| 内乡县| 页游| 诸城市| 深水埗区| 崇义县| 翁源县| 安国市| 图们市| 泌阳县| 鱼台县| 汕尾市| 钟祥市| 内黄县| 桓台县|