org.springframework.web.context
Class ContextLoader

public class ContextLoader
Performs the actual initialization work for the root application context. Called by ContextLoaderListener and ContextLoaderServlet.

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

Passes a "contextConfigLocation" context-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "applicationContext1.xml, applicationContext2.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 earlier 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.

Since17.02.2003
VersionNot specified.
AuthorJuergen Hoeller, Colin Sampaleanu
Wiki javadoc Use textile entry format.
Add your comments here.
Field Summary
static String CONFIG_LOCATION_PARAM
Name of servlet context parameter that can specify the config location for the root context, falling back to the implementation's default otherwise.
static String CONTEXT_CLASS_PARAM
Config param for the root WebApplicationContext implementation class to use: "contextClass"
static String LOCATOR_FACTORY_KEY_PARAM
Optional servlet context parameter used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext) .
static String LOCATOR_FACTORY_SELECTOR_PARAM
Optional servlet context parameter used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext) .
Constructor Summary
ContextLoader()
No description provided.
Method Summary
void closeWebApplicationContext( ServletContext servletContext )
Close Spring's web application context for the given servlet context.
protected WebApplicationContext createWebApplicationContext( ServletContext servletContext, ApplicationContext parent )
Instantiate the root WebApplicationContext for this loader, either the default context class or a custom context class if specified.
protected Class determineContextClass( ServletContext servletContext )
Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.
WebApplicationContext initWebApplicationContext( ServletContext servletContext )
Initialize Spring's web application context for the given servlet context, according to the "contextClass" and "contextConfigLocation" context-params.
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.
CONFIG_LOCATION_PARAM
public static String CONFIG_LOCATION_PARAM
Name of servlet context parameter that can specify the config location for the root context, falling back to the implementation's default otherwise.
Wiki javadoc Use textile entry format.
Add your comments here.
CONTEXT_CLASS_PARAM
public static String CONTEXT_CLASS_PARAM
Config param for the root WebApplicationContext implementation class to use: "contextClass"
Wiki javadoc Use textile entry format.
Add your comments here.
LOCATOR_FACTORY_KEY_PARAM
public static String LOCATOR_FACTORY_KEY_PARAM
Optional servlet context parameter used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext) . Specifies the 'factoryKey' used in the useBeanFactory(String) method call used to obtain the parent application context from the BeanFactoryLocator instance.
Wiki javadoc Use textile entry format.
Add your comments here.
LOCATOR_FACTORY_SELECTOR_PARAM
public static String LOCATOR_FACTORY_SELECTOR_PARAM
Optional servlet context parameter used only when obtaining a parent context using the default implementation of loadParentContext(ServletContext) . Specifies the 'selector' used in the getInstance(String) method call used to obtain the BeanFactoryLocator instance from which the parent context is obtained.

This will normally be set to classpath*:beanRefContext.xml to match the default applied for the getInstance() method.

Wiki javadoc Use textile entry format.
Add your comments here.
ContextLoader
public ContextLoader ( )
No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
closeWebApplicationContext
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
TypeNameDescription
ServletContext servletContext No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
createWebApplicationContext
protected WebApplicationContext createWebApplicationContext ( ServletContext servletContext, 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 ConfigurableWebApplicationContext. Can be overridden in subclasses.

Parameters
TypeNameDescription
ServletContext servletContext current servlet context
ApplicationContext parent the parent ApplicationContext to use, or null if none
Exceptions
BeansException if the context couldn't be initialized
Wiki javadoc Use textile entry format.
Add your comments here.
determineContextClass
protected Class determineContextClass ( ServletContext servletContext )
Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.
Parameters
TypeNameDescription
ServletContext servletContext current servlet context
Exceptions
ApplicationContextException if the context class couldn't be loaded
Wiki javadoc Use textile entry format.
Add your comments here.
initWebApplicationContext
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
TypeNameDescription
ServletContext servletContext current servlet context
Exceptions
IllegalStateException if there is already a root application context present
BeansException if the context failed to initialize
Wiki javadoc Use textile entry format.
Add your comments here.
loadParentContext
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
TypeNameDescription
ServletContext servletContext current servlet context
Exceptions
BeansException if the context couldn't be initialized
Wiki javadoc Use textile entry format.
Add your comments here.