类 ContextLoader
- java.lang.Object
- org.springframework.web.context.ContextLoader
- 直接已知子类:
ContextLoaderListener
public class ContextLoader extends Object
Performs the actual initialization work for the root application context. Called byContextLoaderListener.Looks for a
"contextClass"parameter at theweb.xmlcontext-param level to specify the context class type, falling back toXmlWebApplicationContextif not found. With the default ContextLoader implementation, any context class specified needs to implement theConfigurableWebApplicationContextinterface.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.As of Spring 3.1,
ContextLoadersupports injecting the root web application context via theContextLoader(WebApplicationContext)constructor, allowing for programmatic configuration in Servlet 3.0+ environments. SeeWebApplicationInitializerfor usage examples.- 从以下版本开始:
- 17.02.2003
- 作者:
- Juergen Hoeller, Colin Sampaleanu, Sam Brannen
- 另请参阅:
ContextLoaderListener,ConfigurableWebApplicationContext,XmlWebApplicationContext
字段概要
字段 修饰符和类型 字段 说明 static StringCONFIG_LOCATION_PARAMName 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.static StringCONTEXT_CLASS_PARAMConfig param for the root WebApplicationContext implementation class to use: "contextClass"static StringCONTEXT_ID_PARAMConfig param for the root WebApplicationContext id, to be used as serialization id for the underlying BeanFactory: "contextId"static StringCONTEXT_INITIALIZER_CLASSES_PARAMConfig param forApplicationContextInitializerclasses to use for initializing the root web application context: "contextInitializerClasses"static StringGLOBAL_INITIALIZER_CLASSES_PARAMConfig param for globalApplicationContextInitializerclasses to use for initializing all web application contexts in the current application: "globalInitializerClasses"static StringLOCATOR_FACTORY_KEY_PARAMOptional servlet context parameter (i.e., "parentContextKey") used only when obtaining a parent context using the default implementation ofloadParentContext(ServletContext servletContext).static StringLOCATOR_FACTORY_SELECTOR_PARAMOptional servlet context parameter (i.e., "locatorFactorySelector") used only when obtaining a parent context using the default implementation ofloadParentContext(ServletContext servletContext).
构造器概要
构造器 构造器 说明 ContextLoader()Create a newContextLoaderthat will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params.ContextLoader(WebApplicationContext context)Create a newContextLoaderwith the given application context.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 voidcloseWebApplicationContext(ServletContext servletContext)Close Spring's web application context for the given servlet context.protected voidconfigureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc)protected WebApplicationContextcreateWebApplicationContext(ServletContext sc)Instantiate the root WebApplicationContext for this loader, either the default context class or a custom context class if specified.protected voidcustomizeContext(ServletContext sc, ConfigurableWebApplicationContext wac)Customize theConfigurableWebApplicationContextcreated by this ContextLoader after config locations have been supplied to the context but before the context is refreshed.protected Class<?>determineContextClass(ServletContext servletContext)Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>determineContextInitializerClasses(ServletContext servletContext)Return theApplicationContextInitializerimplementation classes to use if any have been specified byCONTEXT_INITIALIZER_CLASSES_PARAM.static WebApplicationContextgetCurrentWebApplicationContext()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).WebApplicationContextinitWebApplicationContext(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 "contextClass" and "contextConfigLocation" context-params.protected ApplicationContextloadParentContext(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.voidsetContextInitializers(ApplicationContextInitializer<?>... initializers)Specify whichApplicationContextInitializerinstances should be used to initialize the application context used by thisContextLoader.
字段详细资料
CONTEXT_ID_PARAM
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"- 另请参阅:
- 常量字段值
CONFIG_LOCATION_PARAM
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_CLASS_PARAM
public static final String CONTEXT_CLASS_PARAM
Config param for the root WebApplicationContext implementation class to use: "contextClass"
CONTEXT_INITIALIZER_CLASSES_PARAM
public static final String CONTEXT_INITIALIZER_CLASSES_PARAM
Config param forApplicationContextInitializerclasses to use for initializing the root web application context: "contextInitializerClasses"
GLOBAL_INITIALIZER_CLASSES_PARAM
public static final String GLOBAL_INITIALIZER_CLASSES_PARAM
Config param for globalApplicationContextInitializerclasses to use for initializing all web application contexts in the current application: "globalInitializerClasses"
LOCATOR_FACTORY_SELECTOR_PARAM
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 ofloadParentContext(ServletContext servletContext). Specifies the 'selector' used in theContextSingletonBeanFactoryLocator.getInstance(String selector)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 theContextSingletonBeanFactoryLocator.getInstance()method. Supplying the "parentContextKey" parameter is sufficient in this case.- 另请参阅:
- 常量字段值
LOCATOR_FACTORY_KEY_PARAM
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 ofloadParentContext(ServletContext servletContext). Specifies the 'factoryKey' used in theBeanFactoryLocator.useBeanFactory(String factoryKey)method call, obtaining the parent application context from the BeanFactoryLocator instance.Supplying this "parentContextKey" parameter is sufficient when relying on the default
classpath*:beanRefContext.xmlselector for candidate factory references.- 另请参阅:
- 常量字段值
构造器详细资料
ContextLoader
public ContextLoader()
Create a newContextLoaderthat will create a web application context based on the "contextClass" and "contextConfigLocation" servlet context-params. See class-level documentation for details on default values for each.This constructor is typically used when declaring the
ContextLoaderListenersubclass as a<listener>withinweb.xml, as 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_ATTRIBUTEand subclasses are free to call thecloseWebApplicationContext(javax.servlet.ServletContext)method on container shutdown to close the application context.
ContextLoader
public ContextLoader(WebApplicationContext context)
Create a newContextLoaderwith the given application context. This constructor is useful in Servlet 3.0+ environments where instance-based registration of listeners is possible through theServletContext.addListener(java.lang.String)API.The context may or may not yet be refreshed. If it (a) is an implementation of
ConfigurableWebApplicationContextand (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
ServletContextandServletConfigobjects will be delegated to the application contextcustomizeContext(javax.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
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
WebApplicationInitializerfor usage examples.In any case, the given application context will be registered into the ServletContext under the attribute name
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTEand subclasses are free to call thecloseWebApplicationContext(javax.servlet.ServletContext)method on container shutdown to close the application context.- 参数:
context- the application context to manage- 另请参阅:
initWebApplicationContext(ServletContext),closeWebApplicationContext(ServletContext)
方法详细资料
setContextInitializers
public void setContextInitializers(ApplicationContextInitializer<?>... initializers)
Specify whichApplicationContextInitializerinstances should be used to initialize the application context used by thisContextLoader.
initWebApplicationContext
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 "contextClass" and "contextConfigLocation" context-params.- 参数:
servletContext- current servlet context- 返回:
- the new WebApplicationContext
- 另请参阅:
ContextLoader(WebApplicationContext),CONTEXT_CLASS_PARAM,CONFIG_LOCATION_PARAM
createWebApplicationContext
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
ConfigurableWebApplicationContextinterface. Can be overridden in subclasses.In addition,
customizeContext(javax.servlet.ServletContext, org.springframework.web.context.ConfigurableWebApplicationContext)gets called prior to refreshing the context, allowing subclasses to perform custom modifications to the context.- 参数:
sc- current servlet context- 返回:
- the root WebApplicationContext
- 另请参阅:
ConfigurableWebApplicationContext
determineContextClass
protected Class<?> determineContextClass(ServletContext servletContext)
Return the WebApplicationContext implementation class to use, either the default XmlWebApplicationContext or a custom context class if specified.- 参数:
servletContext- current servlet context- 返回:
- the WebApplicationContext implementation class to use
- 另请参阅:
CONTEXT_CLASS_PARAM,XmlWebApplicationContext
configureAndRefreshWebApplicationContext
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc)
customizeContext
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac)
Customize theConfigurableWebApplicationContextcreated 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
ApplicationContextInitializersimplementingOrderedor marked with @Orderwill be sorted appropriately.- 参数:
sc- the current servlet contextwac- the newly created application context- 另请参阅:
CONTEXT_INITIALIZER_CLASSES_PARAM,ApplicationContextInitializer.initialize(ConfigurableApplicationContext)
determineContextInitializerClasses
protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> determineContextInitializerClasses(ServletContext servletContext)
Return theApplicationContextInitializerimplementation classes to use if any have been specified byCONTEXT_INITIALIZER_CLASSES_PARAM.- 参数:
servletContext- current servlet context- 另请参阅:
CONTEXT_INITIALIZER_CLASSES_PARAM
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 viaLOCATOR_FACTORY_SELECTOR_PARAMandLOCATOR_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.- 参数:
servletContext- current servlet context- 返回:
- the parent application context, or
nullif none - 另请参阅:
ContextSingletonBeanFactoryLocator
closeWebApplicationContext
public void closeWebApplicationContext(ServletContext servletContext)
Close Spring's web application context for the given servlet context. If the defaultloadParentContext(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.- 参数:
servletContext- the ServletContext that the WebApplicationContext runs in
getCurrentWebApplicationContext
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).- 返回:
- the current root web application context, or
nullif none found - 另请参阅:
SpringBeanAutowiringSupport