org.springframework.web.context
Class ContextLoader

java.lang.Object
  extended byorg.springframework.web.context.ContextLoader

public class ContextLoader
extends java.lang.Object

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

Regards a "contextClass" parameter at the web.xml context-param level, falling back to the default context class (XmlWebApplicationContext) if not found. With the default ContextLoader, a context class 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.

Since:
17.02.2003
Author:
Juergen Hoeller, Colin Sampaleanu
See Also:
ContextLoaderListener, ContextLoaderServlet, ConfigurableWebApplicationContext, XmlWebApplicationContext

Field Summary
static java.lang.String CONFIG_LOCATION_PARAM
          Name of servlet context parameter that can specify the config location for the root context, falling back to DEFAULT_CONFIG_LOCATION.
static java.lang.String CONTEXT_CLASS_PARAM
          Config param for the root WebApplicationContext implementation class to use: "contextClass"
static java.lang.Class DEFAULT_CONTEXT_CLASS
          Default context class for ContextLoader.
 
Constructor Summary
ContextLoader()
           
 
Method Summary
 void closeWebApplicationContext(javax.servlet.ServletContext servletContext)
          Close Spring's web application context for the given servlet context.
protected  WebApplicationContext createWebApplicationContext(javax.servlet.ServletContext servletContext, ApplicationContext parent)
          Instantiate the root WebApplicationContext for this loader, either a default XmlWebApplicationContext or a custom context class if specified.
 WebApplicationContext initWebApplicationContext(javax.servlet.ServletContext servletContext)
          Initialize Spring's web application context for the given servlet context, regarding the "contextClass" and "contextConfigLocation" context-params.
protected  ApplicationContext loadParentContext(javax.servlet.ServletContext servletContext)
          Template method 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 it is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTEXT_CLASS_PARAM

public static final java.lang.String CONTEXT_CLASS_PARAM
Config param for the root WebApplicationContext implementation class to use: "contextClass"

See Also:
Constant Field Values

DEFAULT_CONTEXT_CLASS

public static final java.lang.Class DEFAULT_CONTEXT_CLASS
Default context class for ContextLoader.

See Also:
XmlWebApplicationContext

CONFIG_LOCATION_PARAM

public static final java.lang.String CONFIG_LOCATION_PARAM
Name of servlet context parameter that can specify the config location for the root context, falling back to DEFAULT_CONFIG_LOCATION.

See Also:
Constant Field Values
Constructor Detail

ContextLoader

public ContextLoader()
Method Detail

initWebApplicationContext

public WebApplicationContext initWebApplicationContext(javax.servlet.ServletContext servletContext)
                                                throws BeansException
Initialize Spring's web application context for the given servlet context, regarding the "contextClass" and "contextConfigLocation" context-params.

Parameters:
servletContext - current servlet context
Returns:
the new WebApplicationContext
Throws:
BeansException - if the context couldn't be initialized
See Also:
CONTEXT_CLASS_PARAM, CONFIG_LOCATION_PARAM

createWebApplicationContext

protected WebApplicationContext createWebApplicationContext(javax.servlet.ServletContext servletContext,
                                                            ApplicationContext parent)
                                                     throws BeansException
Instantiate the root WebApplicationContext for this loader, either a default XmlWebApplicationContext or a custom context class if specified. This implementation expects custom contexts to implement ConfigurableWebApplicationContext. Can be overridden in subclasses.

Throws:
BeansException - if the context couldn't be initialized
See Also:
CONTEXT_CLASS_PARAM, DEFAULT_CONTEXT_CLASS, ConfigurableWebApplicationContext, XmlWebApplicationContext

loadParentContext

protected ApplicationContext loadParentContext(javax.servlet.ServletContext servletContext)
                                        throws BeansException
Template method 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 it is not null.

Parameters:
servletContext -
Returns:
the parent application context, or null if none
Throws:
BeansException - if the context couldn't be initialized

closeWebApplicationContext

public void closeWebApplicationContext(javax.servlet.ServletContext servletContext)
                                throws ApplicationContextException
Close Spring's web application context for the given servlet context.

Parameters:
servletContext - current servlet context
Throws:
ApplicationContextException


Copyright (C) 2003-2004 The Spring Framework Project.