简体   繁体   中英

How is ContextLoaderListener from Spring being run even when web.xml does not include it?

I have a webapp I inherited from another team that uses spring. I have been trying to debug some odd behavior and wanted to "turn off" any spring servlets/filters/context-listeners.

So I removed the entry in web.xml that looked like this...

<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>

Yet somehow after a clean/build and run of our application I am hitting org.springframework.web.context.ContextLoaderListener on the method contextInitialized.

So my question is if my web.xml no longer declared ContextLoadListener as a listener to run how/why is it being run? I looked and the source for Spring 3.2.3-RELEASE does not appear to have the Servlet 3.0 Annotation @WebServletContextListener.

So why/how is this Context Listener running?

在此处输入图片说明

Servlet 3.0 introduced 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.

If you have the spring-web jar on your classpath, it implicitly registers its own implementation of this interface, SpringServletContainerInitializer . This in turn scans for implementations of WebApplicationInitializer on the classpath.

You apparently have SpringWebApplicationInitializer which

[...] initializes Spring context by adding a Spring ContextLoaderListener to the ServletContext .

The ContextLoaderListener you see most likely comes from that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM