ContextLoaderListener.
Looks for a "contextClass" parameter at the
web.xml context-param level to specify the context class type, falling
back to org.
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.
ContextLoader supports injecting the root web application context
via the ContextLoader(WebApplicationContext) constructor, allowing for
programmatic configuration in Servlet initializers. See
org. for usage examples.
ContextLoaderListener, ConfigurableWebApplicationContext, org.springframework.web.context.support.XmlWebApplicationContext
| Modifier and Type | Field and Description |
|---|---|
| public static final String | CONFIG_LOCATION_PARAM
Name of servlet context parameter (i.e., contextConfigLocation) that can specify the config location for the root context, falling back to the implementation's default otherwise. |
| private WebApplicationContext | context
The root WebApplicationContext instance that this loader manages. |
| public static final String | CONTEXT_CLASS_PARAM
Config param for the root WebApplicationContext implementation class to use: contextClass. |
| public static final String | CONTEXT_ID_PARAM
Config param for the root WebApplicationContext id, to be used as serialization id for the underlying BeanFactory: contextId. |
| public static final String | CONTEXT_INITIALIZER_CLASSES_PARAM
Config param for |
| private final List | contextInitializers
Actual ApplicationContextInitializer instances to apply to the context. |
| private static volatile WebApplicationContext | currentContext
The 'current' WebApplicationContext, if the ContextLoader class is deployed in the web app ClassLoader itself. |
| private static final Map | currentContextPerThread
Map from (thread context) ClassLoader to corresponding 'current' WebApplicationContext. |
| private static final String | DEFAULT_STRATEGIES_PATH
Name of the class path resource (relative to the ContextLoader class) that defines ContextLoader's default strategy names. |
| private static Properties | |
| public static final String | GLOBAL_INITIALIZER_CLASSES_PARAM
Config param for global |
| private static final String | INIT_PARAM_DELIMITERS
Any number of these characters are considered delimiters between multiple values in a single init-param String value. |
| Access | Constructor and Description |
|---|---|
| public | ContextLoader()
Create a new |
| public | ContextLoader(WebApplicationContext
the application context to manage context)Create a new |
| Modifier and Type | Method and Description |
|---|---|
| public void | closeWebApplicationContext(ServletContext
the ServletContext that the WebApplicationContext runs in servletContext)Close Spring's web application context for the given servlet context. |
| protected void | |
| protected WebApplicationContext | Returns: the root WebApplicationContextcurrent servlet context sc)Instantiate the root WebApplicationContext for this loader, either the default context class or a custom context class if specified. |
| protected void | customizeContext(ServletContext
the current servlet context sc, ConfigurableWebApplicationContext the newly created application context wac)Customize the |
| protected Class | Returns: the WebApplicationContext implementation class to usecurrent servlet context servletContext)Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified. |
| protected List | determineContextInitializerClasses(ServletContext
current servlet context servletContext)Return the |
| public static WebApplicationContext | Returns: the current root web application context, ornull
if none foundObtain 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). |
| public WebApplicationContext | Returns: the new WebApplicationContextcurrent servlet context servletContext)Initialize Spring's web application context for the given servlet context,
using the application context provided at construction time, or creating a new one
according to the " |
| private Class | |
| protected ApplicationContext | Returns: the parent application context, ornull if nonecurrent servlet context 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. |
| public void | setContextInitializers(ApplicationContextInitializer<?>... initializers)
Specify which |
| CONFIG_LOCATION_PARAM | back to summary |
|---|---|
| public static final String CONFIG_LOCATION_PARAM Name of servlet context parameter (i.e., contextConfigLocation) that can specify the config location for the root context, falling back to the implementation's default otherwise. | |
| context | back to summary |
|---|---|
| private WebApplicationContext context The root WebApplicationContext instance that this loader manages. | |
| CONTEXT_CLASS_PARAM | back to summary |
|---|---|
| public static final String CONTEXT_CLASS_PARAM Config param for the root WebApplicationContext implementation class to use: contextClass. | |
| CONTEXT_ID_PARAM | back to summary |
|---|---|
| public static final String CONTEXT_ID_PARAM Config param for the root WebApplicationContext id, to be used as serialization id for the underlying BeanFactory: contextId. | |
| CONTEXT_INITIALIZER_CLASSES_PARAM | back to summary |
|---|---|
| public static final String CONTEXT_INITIALIZER_CLASSES_PARAM Config param for | |
| contextInitializers | back to summary |
|---|---|
| private final List<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers Actual ApplicationContextInitializer instances to apply to the context. | |
| currentContext | back to summary |
|---|---|
| private static volatile WebApplicationContext currentContext The 'current' WebApplicationContext, if the ContextLoader class is deployed in the web app ClassLoader itself. | |
| currentContextPerThread | back to summary |
|---|---|
| private static final Map<ClassLoader, WebApplicationContext> currentContextPerThread Map from (thread context) ClassLoader to corresponding 'current' WebApplicationContext. | |
| DEFAULT_STRATEGIES_PATH | back to summary |
|---|---|
| private static final String DEFAULT_STRATEGIES_PATH Name of the class path resource (relative to the ContextLoader class) that defines ContextLoader's default strategy names. | |
| defaultStrategies | back to summary |
|---|---|
| private static Properties defaultStrategies
| |
| GLOBAL_INITIALIZER_CLASSES_PARAM | back to summary |
|---|---|
| public static final String GLOBAL_INITIALIZER_CLASSES_PARAM Config param for global | |
| INIT_PARAM_DELIMITERS | back to summary |
|---|---|
| private static final String INIT_PARAM_DELIMITERS Any number of these characters are considered delimiters between multiple values in a single init-param String value. | |
| ContextLoader | back to summary |
|---|---|
| public ContextLoader() Create a new This constructor is typically used when declaring the The created application context will be registered into the ServletContext under
the attribute name | |
| ContextLoader | back to summary |
|---|---|
| public ContextLoader(WebApplicationContext context) Create a new The context may or may not yet be refreshed. If it (a) is an implementation
of
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 In any case, the given application context will be registered into the
ServletContext under the attribute name
| |
| closeWebApplicationContext | back to summary |
|---|---|
| public void closeWebApplicationContext(ServletContext servletContext) Close Spring's web application context for the given servlet context. If overriding
| |
| configureAndRefreshWebApplicationContext | back to summary |
|---|---|
| protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) | |
| createWebApplicationContext | back to summary |
|---|---|
| protected WebApplicationContext createWebApplicationContext(ServletContext sc) 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
In addition,
| |
| customizeContext | back to summary |
|---|---|
| protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) Customize the 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
| |
| determineContextClass | back to summary |
|---|---|
| protected Class Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.
| |
| determineContextInitializerClasses | back to summary |
|---|---|
| protected List Return the
| |
| getCurrentWebApplicationContext | back to summary |
|---|---|
| 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).
| |
| initWebApplicationContext | back to summary |
|---|---|
| public WebApplicationContext initWebApplicationContext(ServletContext servletContext) Initialize Spring's web application context for the given servlet context,
using the application context provided at construction time, or creating a new one
according to the "
| |
| loadInitializerClass | back to summary |
|---|---|
private Class
| |
| loadParentContext | back to summary |
|---|---|
| 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 simply returns
| |
| setContextInitializers | back to summary |
|---|---|
| public void setContextInitializers(ApplicationContextInitializer<?>... initializers) Specify which
| |