Spring ContextLoaderListener source code analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.