如果我们以编程方式创建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个上下文?
感谢。
答案 0 :(得分:1)
仅创建一个上下文 - 仅存在一个根应用程序上下文。
Bootstrap监听器启动并关闭Spring的根WebApplicationContext。
如果您查看ContextLoader
的代码 - 它会使用WebApplicationContext
参数创建contextConfigLocation
(稍后会根据上下文进行解析)
答案 1 :(得分:1)
ContextLoaderListener
只创建一个应用程序上下文,其中包含contextConfigLocation
中所选文件的所有bean。 Bean定义合并在一起,形成一个上下文。
但是,如果您使用Spring MVC,框架将为每个DispatcherServlet
创建一个额外的子上下文。