【发布时间】:2011-02-07 06:57:53
【问题描述】:
我知道getWebApplicationContext()通过ContextLoaderListener或者ContextLoaderServlet找到根WebApplicationContext
但我需要知道区别以及何时使用它?
【问题讨论】:
我知道getWebApplicationContext()通过ContextLoaderListener或者ContextLoaderServlet找到根WebApplicationContext
但我需要知道区别以及何时使用它?
【问题讨论】:
ContextLoaderServlet 的 javadoc 说明了一切:
请注意,对于实现 Servlet API 2.4 或更高版本的容器,该类已被弃用,取而代之的是 ContextLoaderListener。
显然,在 Servlet API 2.4 之前,监听器与 servlet 的初始化顺序并不受规范的强制。因此,要确保 Spring 上下文在 Servlet 2.3 及更低版本容器中的任何其他 servlet 之前正确加载,您需要使用 ContextLoaderServlet 并将其作为启动时加载的第一个。查看该链接了解更多详情。
【讨论】:
上下文加载器加载上下文配置文件 ex(在 web.xml 中):
<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>
【讨论】:
ContextLoadListener 和ContextLoadServlet,两者都调用ContextLoader——负责加载上下文配置文件的类。 laz 的回答更直接针对 op 的原始问题。