Load Spring when the Web Container starts

Source: Internet
Author: User

 

When the following configuration information is made in the Application web. xml, the Spring container is automatically loaded when the Web container is started.

 

<Listener>

 

<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>

 

</Listener>

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

Related Article

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.