隨筆 - 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 閱讀(2575) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 六枝特区| 崇信县| 乡宁县| 郎溪县| 东宁县| 星子县| 吴堡县| 嵊州市| 太仓市| 陆川县| 临桂县| 资阳市| 江安县| 上林县| 抚州市| 远安县| 游戏| 建水县| 瓮安县| 邳州市| 阿勒泰市| 蓬溪县| 探索| 洪泽县| 绿春县| 三江| 高碑店市| 天全县| 甘德县| 尼勒克县| 张家界市| 武宣县| 遵义县| 观塘区| 昌吉市| 金平| 周至县| 巧家县| 苍山县| 遂川县| 昌吉市|