隨筆 - 11, 文章 - 1, 評論 - 20, 引用 - 0
          數據加載中……

          spring配置中bean的循環引用問題及解決方法

          問題:Spring+Hibernate的應用中,定義了兩個業務Service,這里分別稱它們為serivceA,ServiceB。
          它們的關系簡單點來說是這樣的:
          serviceA需要引用serviceB,在serviceB中定義了一個接口列表,serverA必須在serviceB初始化時設置進列表。
          在純bean的情況下,也就是這兩個類不需要設置其他bean的情況下,循環引用是正常的,可以通過的。例如下面配置所表示:

              <bean id="serviceA" class="A"  autowire="byName"  lazy-init="true">
               <property name="serviceB"><ref local="serviceB"/></property>
              </bean>
           <bean id="serviceB" class="B"  autowire="byName"  lazy-init="true">
               <property name="serviceA"><ref bean="serviceA"/></property>
           </bean>
          但是作為一個業務接口,它應該是不需要關心事務,回滾這些無關的東西,
          但現實又有這樣的需求,所以我們必須保證透明的實現這個功能,于是引
          入了AOP方式解決該問題,利用的是Spring自帶的org.springframework.t
          ransaction.interceptor.TransactionProxyFactoryBean.
          重新聲明文件如下:
             <bean id="baseTxProxy" lazy-init="true"
                class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                  <property name="proxyTargetClass"><value>true</value></property>
                  <property name="transactionAttributes">
                      <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
                      </props>
                  </property>
              </bean>
               
              <bean id="serviceA" parent="baseTxProxy">
               <property name="target"><ref local="serviceAImpl"/></property>
              </bean>
             
             <bean id="serviceAImpl" class="serviceA"  autowire="byName"  lazy-init="true">
               <property name="serviceB">
                   <ref bean="serviceB"/>
               </property>
             </bean>
             
              <bean id="serviceB" parent="baseTxProxy" lazy-init="true">
               <property name="target"><ref local="serviceBImpl"/></property>
              </bean>
            
             <bean id="serviceBImpl" class="D" lazy-init="true">
               <property name="serviceA">
                   <ref bean="serviceA"/>
               </property>
             </bean>
          于是問題就出現了,Spring報了FactoryBeanCircularReferenceException,無法繼續完成設置工作。
          查看TransactionProxyFactoryBean源碼,其實現了FactoryBean和InitializingBean接口,應該是
          做了代理之后,兩個代理Bean需要等待所有Bean設置完成后才會標識狀態為初始化完畢,于是造成了
          沖突。

              由于兩個業務服務互相調用的路徑是不相交的,所以采用了一種變通的方法,在聲明serviceA時,
          直接定義serviceB:
            <bean id="serviceAImpl" class="serviceA"  autowire="byName"  lazy-init="true">
               <property name="serviceB">
                   <bean class="B"  autowire="byName"/>
               </property>
           </bean>
          相當于serviceB和serviceA中使用的serviceB不是同一個實例。
           
           但是如果確實調用重合時怎么辦?
           
           解決方法是這樣的:
           
           <bean id="serviceAImpl" class="serviceA"  autowire="byName"  lazy-init="true">
               <property name="serviceB">
                   <ref bean="serviceBImpl"/>
               </property>
           </bean>
           
            非常簡單,serviceAImpl調用時,可能已經在事務環境中了,不需再使用serviceB代理的事務支持,
            于是直接引用serviceB實例。這個方法是我寫這篇文章時想到的,-_-!!!,看來知識果真還是好好
            整理呀。

           

          posted on 2006-02-07 08:53 wolfsquare 閱讀(2573) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 宾川县| 开平市| 东港市| 鄯善县| 金平| 无棣县| 邵阳县| 措勤县| 大同市| 镇安县| 武川县| 沿河| 资兴市| 黔南| 平江县| 苗栗市| 紫金县| 榆树市| 衡阳县| 玛多县| 仪征市| 尼勒克县| 姜堰市| 桐梓县| 临泽县| 焉耆| 古浪县| 扎鲁特旗| 临湘市| 宜章县| 蓬安县| 梁河县| 峨眉山市| 嵩明县| 昌宁县| 宜章县| 建瓯市| 客服| 宁武县| 济源市| 江达县|