Package org.springframework.web.context

# Class ContextLoaderListener

java.lang.Object
org.springframework.web.context.ContextLoader
org.springframework.web.context.ContextLoaderListener

All Implemented Interfaces:
  `ServletContextListener`, `EventListener`

public class ContextLoaderListener extends ContextLoader implements ServletContextListener
Bootstrap listener to start up and shut down Spring's root `WebApplicationContext`. Simply delegates to `ContextLoader` as well as to `ContextCleanupListener`.

`ContextLoaderListener` supports injecting the root web application context via the `ContextLoaderListener(WebApplicationContext)` constructor, allowing for programmatic configuration in Servlet initializers. See `WebApplicationInitializer` for usage examples.

Since:
  17.02.2003
Author:
  Juergen Hoeller, Chris Beams
See Also:
  - `ContextLoader.setContextInitializers(org.springframework.context.ApplicationContextInitializer<?>...)`
  - `WebApplicationInitializer`

## Field Summary

### Fields inherited from class org.springframework.web.context.ContextLoader

`CONFIG_LOCATION_PARAM, CONTEXT_CLASS_PARAM, CONTEXT_ID_PARAM, CONTEXT_INITIALIZER_CLASSES_PARAM, GLOBAL_INITIALIZER_CLASSES_PARAM`

## Constructor Summary

Constructors
Constructor
Description
`ContextLoaderListener()`
Create a new `ContextLoaderListener` that will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params.
`ContextLoaderListener(WebApplicationContext rootContext)`
Create a new `ContextLoaderListener` with the given application context, initializing it with the `ServletContextEvent`-provided `ServletContext` reference which is spec-restricted in terms of capabilities.
`ContextLoaderListener(WebApplicationContext rootContext, ServletContext servletContext)`
Create a new `ContextLoaderListener` with the given application context.

## Method Summary

Modifier and Type
Method
Description
`void`
`contextDestroyed(ServletContextEvent event)`
Close the root web application context.
`void`
`contextInitialized(ServletContextEvent event)`
Initialize the root web application context.

### Methods inherited from class org.springframework.web.context.ContextLoader

`closeWebApplicationContext, configureAndRefreshWebApplicationContext, createWebApplicationContext, customizeContext, determineContextClass, determineContextInitializerClasses, getCurrentWebApplicationContext, initWebApplicationContext, loadParentContext, setContextInitializers`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ContextLoaderListener

public 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(jakarta.servlet.ServletContextEvent)` lifecycle method is invoked on this listener.

See Also:
  - `ContextLoader`
  - `ContextLoaderListener(WebApplicationContext)`
  - `contextInitialized(ServletContextEvent)`
  - `contextDestroyed(ServletContextEvent)`

### ContextLoaderListener

public ContextLoaderListener(WebApplicationContext rootContext)
Create a new `ContextLoaderListener` with the given application context, initializing it with the `ServletContextEvent`-provided `ServletContext` reference which is spec-restricted in terms of capabilities.

It is generally preferable to initialize the application context with a `WebApplicationInitializer.onStartup(jakarta.servlet.ServletContext)`-given reference which is usually fully capable.

See Also:
  - `ContextLoaderListener(WebApplicationContext, ServletContext)`

### ContextLoaderListener

public ContextLoaderListener(WebApplicationContext rootContext, ServletContext servletContext)
Create a new `ContextLoaderListener` with the given application context. This constructor is useful in Servlet initializers where instance-based registration of listeners is possible through the `ServletContext.addListener(java.lang.String)` API.

The context may or may not yet be refreshed. 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 id, one will be assigned to it
- `ServletContext` and `ServletConfig` objects will be delegated to the application context
- `ContextLoader.customizeContext(jakarta.servlet.ServletContext, org.springframework.web.context.ConfigurableWebApplicationContext)` will be called
- Any `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 `WebApplicationInitializer` for usage examples.

In any case, the given 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(jakarta.servlet.ServletContextEvent)` lifecycle method is invoked on this listener.

Parameters:
  `rootContext` - the application context to manage
  `servletContext` - the ServletContext to initialize with
Since:
  6.2
See Also:
  - `contextInitialized(ServletContextEvent)`
  - `contextDestroyed(ServletContextEvent)`

## Method Details

### contextInitialized

public void contextInitialized(ServletContextEvent event)
Initialize the root web application context.

Specified by:
  `contextInitialized` in interface `ServletContextListener`

### contextDestroyed

public void contextDestroyed(ServletContextEvent event)
Close the root web application context.

Specified by:
  `contextDestroyed` in interface `ServletContextListener`