我有一个从另一个使用 spring 的团队继承的 web 应用程序。我一直在尝试调试一些奇怪的行为,并想“关闭”任何 Spring servlet/filters/context-listeners。
所以我删除了 web.xml 中看起来像这样的条目...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
然而,在清理/构建并运行我们的应用程序之后,我在方法 contextInitialized 上点击了 org.springframework.web.context.ContextLoaderListener。
所以我的问题是,如果我的 web.xml 不再声明 ContextLoadListener 作为监听器来运行它如何/为什么运行?我查看了 Spring 3.2.3-RELEASE 的源代码似乎没有 Servlet 3.0 Annotation @WebServletContextListener。
那么这个上下文监听器为什么/如何运行?
最佳答案
Servlet 3.0 推出 ServletContainerInitializer
Interface which allows a library/runtime to be notified of a web application's startup phase and perform any required programmatic registration of servlets, filters, and listeners in response to it.
如果您的类路径中有 spring-web jar,它会隐式注册该接口(interface)的自己的实现 SpringServletContainerInitializer
。这反过来会扫描 WebApplicationInitializer
的实现在类路径上。
你显然有SpringWebApplicationInitializer
其中
[...] initializes Spring context by adding a Spring
ContextLoaderListener
to theServletContext
.
您看到的ContextLoaderListener
很可能来自于此。
关于java - 即使 web.xml 不包含 Spring 的 ContextLoaderListener,它又如何运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36924863/