benstpierre
benstpierre

Reputation: 33581

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?

enter image description here

Upvotes: 1

Views: 1723

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

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.

Upvotes: 2

Related Questions