1. 程式人生 > >spring context 繼承

spring context 繼承

dispatch xmla pri framework class arc 後者 ring b-

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <
listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</
servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet-example.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <
servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

上面的配置文件定義了 2 個 WebApplicationContext,前一個容器的配置文件是 applicationContext.xml, 後一個容器的配置文件是 servlet-example.xml,前者是後者的 parent。

手動創建父子容器:

ClassPathXmlApplicationContext appCtx = 
        new ClassPathXmlApplicationContext("parent.xml");

// 指定父容器
ClassPathXmlApplicationContext childCtx = 
        new ClassPathXmlApplicationContext(new String[]{"child.xml"}, appCtx);

現象:子容器可以獲得父容器中的 bean,反之不能。

spring context 繼承