2011-09-26 24 views
1

如果我們以編程方式創建ApplicationContext實例,很容易知道已創建了多少上下文。但是,如果我們使用ContextLoaderListener,會創建多少個上下文?例如Spring的reference如下:由ContextLoaderListener創建的Spring上下文的數量

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value> 
</context-param> 

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

它有2個上下文xml文件。這是否意味着創建了2個上下文?

謝謝。

回答

1

ContextLoaderListener只創建一個包含從文件中所有的bean應用上下文中contextConfigLocation選擇創建一個WebApplicationContext 。 Bean定義合併在一起形成一個上下文。

但是,如果您使用Spring MVC,該框架將爲每個DispatcherServlet創建一個額外的子上下文。

1

只創建一個上下文 - 只存在一個根應用程序上下文。

Bootstrap偵聽器啓動和關閉Spring的根WebApplicationContext。

如果你看看ContextLoader代碼 - 它使用contextConfigLocation參數(這就是後來由上下文解析)

相關問題