public class

ContextLoader

extends Object
java.lang.Object
   ↳ org.springframework.web.context.ContextLoader
Known Direct Subclasses

Class Overview

Performs the actual initialization work for the root application context. Called by ContextLoaderListener.

Looks for a "contextClass" parameter at the web.xml context-param level to specify the context class type, falling back to the default of XmlWebApplicationContext if not found. With the default ContextLoader implementation, any context class specified needs to implement the ConfigurableWebApplicationContext interface.

Processes a "contextConfigLocation" context-param and passes its value to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, e.g. "WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml". Ant-style path patterns are supported as well, e.g. "WEB-INF/*Context.xml,WEB-INF/spring*.xml" or "WEB-INF/**/*Context.xml". If not explicitly specified, the context implementation is supposed to use a default location (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").

Note: In case of multiple config locations, later bean definitions will override ones defined in previously loaded files, at least when using one of Spring's default ApplicationContext implementations. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

Above and beyond loading the root application context, this class can optionally load or obtain and hook up a shared parent context to the root application context. See the loadParentContext(ServletContext) method for more information.

Summary

Constants
String CONFIG_LOCATION_PARAM Name of servlet context parameter (i.e., {@value }) that can specify the config location for the root context, falling back to the implementation's default otherwise.
String CONTEXT_CLASS_PARAM Config param for the root WebApplicationContext implementation class to use: {@value }
String CONTEXT_INITIALIZER_CLASSES_PARAM Config param for which ApplicationContextInitializer classes to use for initializing the web application context: {@value }
String LOCATOR_FACTORY_KEY_PARAM Optional servlet context parameter (i.e., "parentContextKey") used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext).
String LOCATOR_FACTORY_SELECTOR_PARAM Optional servlet context parameter (i.e., "locatorFactorySelector") used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext).
Public Constructors
ContextLoader()
Public Methods
void closeWebApplicationContext(ServletContext servletContext)
Close Spring's web application context for the given servlet context.
static WebApplicationContext getCurrentWebApplicationContext()
Obtain the Spring root web application context for the current thread (i.e.
WebApplicationContext initWebApplicationContext(ServletContext servletContext)
Initialize Spring's web application context for the given servlet context, according to the "contextClass" and "contextConfigLocation" context-params.
Protected Methods
WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent)
Instantiate the root WebApplicationContext for this loader, either the default context class or a custom context class if specified.
void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext)
Customize the ConfigurableWebApplicationContext created by this ContextLoader after config locations have been supplied to the context but before the context is refreshed.
Class<?> determineContextClass(ServletContext servletContext)
Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.
List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> determineContextInitializerClasses(ServletContext servletContext)
Return the ApplicationContextInitializer implementation classes to use if any have been specified by CONTEXT_INITIALIZER_CLASSES_PARAM.
ApplicationContext loadParentContext(ServletContext servletContext)
Template method with default implementation (which may be overridden by a subclass), to load or obtain an ApplicationContext instance which will be used as the parent context of the root WebApplicationContext.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String CONFIG_LOCATION_PARAM

Name of servlet context parameter (i.e., {@value }) that can specify the config location for the root context, falling back to the implementation's default otherwise.

Constant Value: "contextConfigLocation"

public static final String CONTEXT_CLASS_PARAM

Config param for the root WebApplicationContext implementation class to use: {@value }

Constant Value: "contextClass"

public static final String CONTEXT_INITIALIZER_CLASSES_PARAM

Config param for which ApplicationContextInitializer classes to use for initializing the web application context: {@value }

Constant Value: "contextInitializerClasses"

public static final String LOCATOR_FACTORY_KEY_PARAM

Optional servlet context parameter (i.e., "parentContextKey") used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext). Specifies the 'factoryKey' used in the useBeanFactory(String) method call, obtaining the parent application context from the BeanFactoryLocator instance.

Supplying this "parentContextKey" parameter is sufficient when relying on the default classpath*:beanRefContext.xml selector for candidate factory references.

Constant Value: "parentContextKey"

public static final String LOCATOR_FACTORY_SELECTOR_PARAM

Optional servlet context parameter (i.e., "locatorFactorySelector") used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext). Specifies the 'selector' used in the getInstance(String) method call, which is used to obtain the BeanFactoryLocator instance from which the parent context is obtained.

The default is classpath*:beanRefContext.xml, matching the default applied for the getInstance() method. Supplying the "parentContextKey" parameter is sufficient in this case.

Constant Value: "locatorFactorySelector"

Public Constructors

public ContextLoader ()

Public Methods

public void closeWebApplicationContext (ServletContext servletContext)

Close Spring's web application context for the given servlet context. If the default loadParentContext(ServletContext) implementation, which uses ContextSingletonBeanFactoryLocator, has loaded any shared parent context, release one reference to that shared parent context.

If overriding loadParentContext(ServletContext), you may have to override this method as well.

Parameters
servletContext the ServletContext that the WebApplicationContext runs in

public static WebApplicationContext getCurrentWebApplicationContext ()

Obtain the Spring root web application context for the current thread (i.e. for the current thread's context ClassLoader, which needs to be the web application's ClassLoader).

Returns
  • the current root web application context, or null if none found

public WebApplicationContext initWebApplicationContext (ServletContext servletContext)

Initialize Spring's web application context for the given servlet context, according to the "contextClass" and "contextConfigLocation" context-params.

Parameters
servletContext current servlet context
Returns
  • the new WebApplicationContext

Protected Methods

protected WebApplicationContext createWebApplicationContext (ServletContext sc, ApplicationContext parent)

Instantiate the root WebApplicationContext for this loader, either the default context class or a custom context class if specified.

This implementation expects custom contexts to implement the ConfigurableWebApplicationContext interface. Can be overridden in subclasses.

In addition, customizeContext(ServletContext, ConfigurableWebApplicationContext) gets called prior to refreshing the context, allowing subclasses to perform custom modifications to the context.

Parameters
sc current servlet context
parent the parent ApplicationContext to use, or null if none
Returns
  • the root WebApplicationContext

protected void customizeContext (ServletContext servletContext, ConfigurableWebApplicationContext applicationContext)

Customize the ConfigurableWebApplicationContext created by this ContextLoader after config locations have been supplied to the context but before the context is refreshed.

The default implementation determines what (if any) context initializer classes have been specified through context init parameters and invokes each with the given web application context.

Any Ordered ApplicationContextInitializer will be sorted appropriately.

Parameters
servletContext the current servlet context
applicationContext the newly created application context

protected Class<?> determineContextClass (ServletContext servletContext)

Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.

Parameters
servletContext current servlet context
Returns
  • the WebApplicationContext implementation class to use

protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> determineContextInitializerClasses (ServletContext servletContext)

Return the ApplicationContextInitializer implementation classes to use if any have been specified by CONTEXT_INITIALIZER_CLASSES_PARAM.

Parameters
servletContext current servlet context

protected ApplicationContext loadParentContext (ServletContext servletContext)

Template method with default implementation (which may be overridden by a subclass), to load or obtain an ApplicationContext instance which will be used as the parent context of the root WebApplicationContext. If the return value from the method is null, no parent context is set.

The main reason to load a parent context here is to allow multiple root web application contexts to all be children of a shared EAR context, or alternately to also share the same parent context that is visible to EJBs. For pure web applications, there is usually no need to worry about having a parent context to the root web application context.

The default implementation uses ContextSingletonBeanFactoryLocator, configured via LOCATOR_FACTORY_SELECTOR_PARAM and LOCATOR_FACTORY_KEY_PARAM, to load a parent context which will be shared by all other users of ContextsingletonBeanFactoryLocator which also use the same configuration parameters.

Parameters
servletContext current servlet context
Returns
  • the parent application context, or null if none