ContextLoaderListener和ContextLoaderServlet的区别

9
我知道getWebApplicationContext()通过ContextLoaderListener或ContextLoaderServlet找到根WebApplicationContext。
但我需要知道它们之间的区别以及何时使用?
2个回答

14

ContextLoaderServlet的javadoc已经说明了一切:

请注意,在实现Servlet API 2.4或更高版本的容器中,该类已被弃用,而应使用ContextLoaderListener。

显然,在Servlet API 2.4之前,监听器与servlet初始化顺序不受规范约束。因此,为了确保Spring上下文在Servlet 2.3及更低版本的容器中正确加载,您需要使用ContextLoaderServlet并将其置于第一个启动。详细信息请参见该链接。


2
一个上下文加载器负责加载上下文配置文件,例如(在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>

这个问题是关于ContextLoadListenerContextLoadServlet的,它们都调用ContextLoader -- 这个类负责加载上下文配置文件。laz的回答更直接地回答了原始问题。 - Tung

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