java - 即使 web.xml 不包含 Spring 的 ContextLoaderListener,它又如何运行?

标签 java spring servlets servlet-3.0 servletcontextlistener

我有一个从另一个使用 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。

那么这个上下文监听器为什么/如何运行?

enter image description here

最佳答案

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 the ServletContext.

您看到的ContextLoaderListener很可能来自于此。

关于java - 即使 web.xml 不包含 Spring 的 ContextLoaderListener,它又如何运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36924863/

相关文章:

Java 字符串正则表达式替换方法从替换中删除反斜杠

java - 如何通过 spring-data 为 @ManyToMany 加载获取选择?

java - Spring Security 显示有效用户的访问被拒绝

java - 内联抽象类型时根据子类使用不同的投影

eclipse - IllegalArgumentException : The servlets named [AdminController] and [muShag. Private.controller.AdminController] 都映射到 url-pattern

java - servlet 是否知道使用 http-equiv 指定的已发送表单的编码?

java - 当部署在服务器上时,这个 Java 应用程序有何变化?

java - 将组件添加到 jPanel java

java - spring.io 中的 Spring Cloud 配置示例不起作用

java - com.sun.proxy.$Proxy219 无法使用 Java Config 进行转换,但它可以与 xml 一起正常工作