Servlet的Spring根WebApplicationContext


问题内容

我有一个使用SpringMVC
DispatcherServlet加载WebApplicationContext的Web应用程序。Spring参考文档

“每个DispatcherServlet都有自己的WebApplicationContext,它继承了已经在
根WebApplicationContext中 定义的所有bean
。这些继承的bean可以在servlet特定的作用域中被覆盖,并且您可以在给定servlet实例本地定义新的特定于作用域的bean。

但是,该根WebApplicationContext放在哪里?


问题答案:

我们applicationContext.xmlWEB-INF目录中,该配置中的Bean可用于该spring- servlet.xml配置,其定义使用

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

顺便说一下ContextLoaderListener,它负责管理根上下文

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