spring-framework / org.springframework.web.context / ContextLoaderListener

ContextLoaderListener

open class ContextLoaderListener : ContextLoader, ServletContextListener

Bootstrap listener to start up and shut down Spring's root WebApplicationContext. Simply delegates to ContextLoader as well as to ContextCleanupListener.

As of Spring 3.1, ContextLoaderListener supports injecting the root web application context via the #ContextLoaderListener(WebApplicationContext) constructor, allowing for programmatic configuration in Servlet 3.0+ environments. See org.springframework.web.WebApplicationInitializer for usage examples.

Author
Juergen Hoeller

Author
Chris Beams

Since
17.02.2003

See Also
#setContextInitializersorg.springframework.web.WebApplicationInitializer

Constructors

<init>

ContextLoaderListener()

Create a new ContextLoaderListener that will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params. See ContextLoader superclass documentation for details on default values for each.

This constructor is typically used when declaring ContextLoaderListener as a <listener> within web.xml, where a no-arg constructor is required.

The created application context will be registered into the ServletContext under the attribute name WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE and the Spring application context will be closed when the #contextDestroyed lifecycle method is invoked on this listener.

ContextLoaderListener(context: WebApplicationContext)

Create a new ContextLoaderListener with the given application context. This constructor is useful in Servlet 3.0+ environments where instance-based registration of listeners is possible through the javax.servlet.ServletContext#addListener API.

The context may or may not yet be . If it (a) is an implementation of ConfigurableWebApplicationContext and (b) has not already been refreshed (the recommended approach), then the following will occur:

  • If the given context has not already been assigned an , one will be assigned to it
  • ServletContext and ServletConfig objects will be delegated to the application context
  • #customizeContext will be called
  • Any org.springframework.context.ApplicationContextInitializers specified through the "contextInitializerClasses" init-param will be applied.
  • refresh() will be called
If the context has already been refreshed or does not implement ConfigurableWebApplicationContext, none of the above will occur under the assumption that the user has performed these actions (or not) per his or her specific needs.

See org.springframework.web.WebApplicationInitializer for usage examples.

In any case, the given application context will be registered into the ServletContext under the attribute name and the Spring application context will be closed when the #contextDestroyed lifecycle method is invoked on this listener.

Functions

contextDestroyed

open fun contextDestroyed(event: ServletContextEvent): Unit

Close the root web application context.

contextInitialized

open fun contextInitialized(event: ServletContextEvent): Unit

Initialize the root web application context.