ContextLoaderListener创建的Spring上下文数

时间:2011-09-26 13:59:25

标签: spring inversion-of-control

如果我们以编程方式创建ApplicationContext个实例,很容易知道创建了多少个上下文。但是,如果我们使用ContextLoaderListener,会创建多少个上下文?例如,Spring的reference如下:

<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个上下文?

感谢。

2 个答案:

答案 0 :(得分:1)

仅创建一个上下文 - 仅存在一个根应用程序上下文。

  

Bootstrap监听器启动并关闭Spring的根WebApplicationContext。

如果您查看ContextLoader的代码 - 它会使用WebApplicationContext参数创建contextConfigLocation(稍后会根据上下文进行解析)

答案 1 :(得分:1)

ContextLoaderListener只创建一个应用程序上下文,其中包含contextConfigLocation中所选文件的所有bean。 Bean定义合并在一起,形成一个上下文。

但是,如果您使用Spring MVC,框架将为每个DispatcherServlet创建一个额外的子上下文。