Spring根WebApplicationContext用于Servlet

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

“每个DispatcherServlet都有自己的WebApplicationContext,它继承了根WebApplicationContext中已定义的所有bean。这些继承的bean可以在特定于servlet的范围内被覆盖,并且您可以定义新的特定于servlet实例的范围特定bean。”

但是我应该把这个根WebApplicationContext放在哪里呢?
1个回答

9

我们在WEB-INF目录下有applicationContext.xml,该配置文件中的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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接