久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

spring的父子容器及配置詳解

spring父子容器

創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元烏蘭做網(wǎng)站,已為上家服務(wù),為烏蘭各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

spring總的上下文容器有父子之分,父容器和子容器。 ** 父容器對子容器可見,子容器對父容器不可見 **。

對于傳統(tǒng)的spring mvc來說,spring mvc容器為子容器,也就是說ServletDispatcher對應(yīng)的容器為子容器,而web.xml中通過ConextLoaderListener的contextConfigLocation屬性配置的為父容器。

父子容器的使用場景

父子容器的主要用途是上下文隔離??紤]以下一種場景。

  1. project-service.jar為服務(wù)層模塊。包含一些數(shù)據(jù)庫service方法。其對應(yīng)的spring配置文件為project-service.xml。
  2. project-api為api服務(wù)器代碼。它依賴于project-service.jar。其對應(yīng)的配置文件為project-api.xml。

project-api需要對project-service里的某些方法進(jìn)行decorate,進(jìn)行裝飾,比如給CustomerService進(jìn)行裝飾。裝飾后的類為CachedCustomerService。于是,現(xiàn)在project-api里面包含兩個CustomerService,一個是來自project-service的CustomerService,另一個是CachedCustomerService。這個時候,如果project-api工程所有的配置文件都通過一個上下文進(jìn)行加載,勢必出現(xiàn)問題(通常的做法是用import標(biāo)簽全部給import進(jìn)來)。因為,project里的PayService里通過@Resource標(biāo)準(zhǔn)注入了CustomerService,類似如下

@Serivce
public class PayService{
@Resource
private CustomerService cusService;
}

解決方式

這時,由于上下文在注入customerService屬性的時候,遇到了兩個CustomService。它無法判讀注入哪個Service。

當(dāng)然了,有人會說,改一下PayService的Resource屬性,指定下具體注入哪個。但是,project-service.jar是第三方庫的話,改動代碼變得不可行,除非拿到源碼。

這個時候,就可以通過父子容器的方式解決這個問題。

將project-service放在父容器中,project-api所有的bean用子容器加載。

假設(shè)project-api的上下文配置文件為project-api.xml,實現(xiàn)方法如下。

1、定義project-total.xml

 <bean id = "serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
  <constructor-arg>
  <value>
   classpath:project-service.xml
  </value>
  </constructor-arg>
 </bean>

 <bean id = "apiContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
  <constructor-arg>
   <value>
    classpath:project-api.xml
   </value>
  </constructor-arg>

  <constructor-arg>
   <ref bean="serviceContext"/>
  </constructor-arg>
 </bean>

2、在web.xml的上下文配置中如下。

 <context-param> 
  <param-name>contextConfigLocation</param-name>
  <param-value> classpath*:project-total.xml</param-value> 
 </context-param>

 <listener> 
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
  </listener>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

serviceContext為父容器,apiContext為子容器,從而實現(xiàn)serviceContext看不到apiContext,而apiContext可以看見serviceContext的效果。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁題目:spring的父子容器及配置詳解
文章起源:http://sd-ha.com/article20/peppco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、關(guān)鍵詞優(yōu)化網(wǎng)站設(shè)計、面包屑導(dǎo)航、全網(wǎng)營銷推廣、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)