When we want to automatically assemble ApplicationContext configuration information, we first configure ContextLoaderListener in web. xml. The following is part of the source code:
[Java]
Public class ContextLoaderListener implements ServletContextListener {
Private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
Public void contextInitialized (ServletContextEvent event ){
This. contextLoader = createContextLoader ();
This. contextLoader. initWebApplicationContext (event. getServletContext (); // note this
}
Protected ContextLoader createContextLoader (){
Return new ContextLoader ();
}
Public ContextLoader getContextLoader (){
Return this. contextLoader;
}
Public void contextDestroyed (ServletContextEvent event ){
If (this. contextLoader! = Null ){
This. contextLoader. closeWebApplicationContext (event. getServletContext ());
}
}
}
This document describes how WebApplicationContext is initialized. First, this. contextLoader. initWebApplicationContext (event. getServletContext () of the contextInitialized (ServletContextEvent event) method.
[Java]
Public WebApplicationContext initWebApplicationContext (ServletContext servletContext)
Throws IllegalStateException, BeansException {
If (servletContext. getAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE )! = Null ){
Throw new IllegalStateException (
"Cannot initialize context because there is already a root application context present-" +
"Check whether you have multiple ContextLoader * definitions in your web. xml! ");
}
ServletContext. log ("Initializing Spring root WebApplicationContext ");
If (logger. isInfoEnabled ()){
Logger.info ("Root WebApplicationContext: initialization started ");
}
Long startTime = System. currentTimeMillis ();
Try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext (servletContext );
// Store context in local instance variable, to guarantee that
// It is available on ServletContext shutdown.
This. context = createWebApplicationContext (servletContext, parent );
ServletContext. setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this. context); // pay attention to this
CurrentContextPerThread. put (Thread. currentThread (). getContextClassLoader (), this. context );
If (logger. isDebugEnabled ()){
Logger. debug ("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
If (logger. isInfoEnabled ()){
Long elapsedTime = System. currentTimeMillis ()-startTime;
Logger.info ("Root WebApplicationContext: initialization completed in" + elapsedTime + "ms ");
}
Return this. context;
}
Catch (RuntimeException ex ){
Logger. error ("Context initialization failed", ex );
ServletContext. setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex );
Throw ex;
}
Catch (Error err ){
Logger. error ("Context initialization failed", err );
ServletContext. setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err );
Throw err;
}
}
Note that in this section, we only need to get the attribute name WebApplicationContext in ServletContext to obtain a WebApplicationContext. you can obtain an ApplicationContext by using the constant value of ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE. In the end, you can obtain any Bean object in the spring configuration file.
Eg:
[Java]
Public String execute () throws Exception {
// TODO Auto-generated method stub
ApplicationContext conn = (ApplicationContext) ServletActionContext. getServletContext (). getAttribute (WebApplicationContext. class. getName () + ". ROOT ");
System. out. println (conn. getBean ("biz "));
Return SUCCESS;
}
You can also use the abstract tool class org. springframework. web. context. support. WebApplicationContextUtils to obtain WebApplicationContext.
[Java]
Public static WebApplicationContext getWebApplicationContext (ServletContext SC ){
Return getWebApplicationContext (SC, WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE );
}
Author: hanzhou4519