When the following configuration information is made in the Application web. xml, the Spring container is automatically loaded when the Web container is started. & Lt; listener & gt; & lt; listener-class & gt; org. springframework. web. context. ContextLoaderListener & lt;/listener-class & gt
When the following configuration information is made in the Application web. xml, the Spring container is automatically loaded when the Web container is started.
Org. springframework. web. context. ContextLoaderListener
The ContextLoaderListener class implements the javax. servlet. ServletContextListener interface and inherits the org. springframework. web. context. ContextLoader class. The ServletContextListener event class is part of a Web container that processes the listener of the Servlet context of a Web application. Implement the contextInitialized and contextDestroyed methods in the ServletContextListener interface. When the Web Container starts, the contextInitialized method is automatically called to initialize the context of the Spring Web application, mainly load the web. contextConfigLocation configuration file in xml. Before the Web container is closed, the contextDestroyed method is called to destroy the context of the Spring Web application. The ContextLoader class implements Spring context initialization and executes the initWebApplicationContext method to return WebApplicationContext. The contextInitialized and contextDestroyed codes implemented by Spring are as follows:
Public void contextInitialized (ServletContextEvent event ){
This. contextLoader = createContextLoader ();
If (this. contextLoader = null ){
This. contextLoader = this;
}
This. contextLoader. initWebApplicationContext (event. getServletContext ());
}
Public void contextDestroyed (ServletContextEvent event ){
If (this. contextLoader! = Null ){
This. contextLoader. closeWebApplicationContext (event. getServletContext ());
}
ContextCleanupListener. cleanupAttributes (event. getServletContext ());
}
Spring Implementation Method
Public WebApplicationContext initWebApplicationContext (ServletContext servletContext ){
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! ");
}
Log logger = LogFactory. getLog (ContextLoader. class );
ServletContext. log ("Initializing Spring root WebApplicationContext ");
If (logger. isInfoEnabled ()){
Logger.info ("Root WebApplicationContext: initialization started ");
}
Long startTime = System. currentTimeMillis ();
Try {
// Store context in local instance variable, to guarantee that
// It is available on ServletContext shutdown.
If (this. context = null ){
This. context = createWebApplicationContext (servletContext );
}
If (this. context instanceof ConfigurableWebApplicationContext ){
ConfigureAndRefreshWebApplicationContext (ConfigurableWebApplicationContext) this. context, servletContext );
}
ServletContext. setAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this. context );
ClassLoader ccl = Thread. currentThread (). getContextClassLoader ();
If (ccl = ContextLoader. class. getClassLoader ()){
CurrentContext = this. context;
}
Else if (ccl! = Null ){
CurrentContextPerThread. put (ccl, 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;
}
}
Blue