How to enable a Spring MVC Web app to perform specific processing when it starts

Source: Internet
Author: User

In the application of ASP. Global.asax in the root directory, in the Application_Start method to do some initialization operations, such as: Pre-loading cache entries to warm up the site hotspot data, to obtain some remote configuration information and so on.

SPRING-MVC applications, to achieve similar functions, mainly by implementing the following interfaces (optionally, at least one)

First, Applicationcontextaware interface

Package Org.springframework.context;import Org.springframework.beans.beansexception;import Org.springframework.beans.factory.aware;import Org.springframework.context.applicationcontext;public Interface Applicationcontextaware extends Aware {    void Setapplicationcontext (ApplicationContext var1) throws Beansexception ;}

Second, Servletcontextaware interface

Package Org.springframework.web.context;import Javax.servlet.servletcontext;import Org.springframework.beans.factory.aware;public interface Servletcontextaware extends Aware {    void Setservletcontext (ServletContext var1);}

Third, Initializingbean interface

Package Org.springframework.beans.factory;public interface Initializingbean {    void Afterpropertiesset () throws Exception;}

Four, applicationlistener<applicationevent> interface

Package Org.springframework.context;import Java.util.eventlistener;import Org.springframework.context.applicationevent;public interface Applicationlistener<e extends ApplicationEvent > extends EventListener {    void onapplicationevent (E var1);}

example program:

Package Test.web.listener;import Org.apache.logging.log4j.*;import Org.springframework.beans.*;import Org.springframework.beans.factory.initializingbean;import Org.springframework.context.*;import Org.springframework.context.event.contextrefreshedevent;import Org.springframework.stereotype.component;import Org.springframework.web.context.servletcontextaware;import Javax.servlet.ServletContext; @Componentpublic class Startuplistener implements Applicationcontextaware, Servletcontextaware, Initializingbean, applicationlistener<c    ontextrefreshedevent> {protected Logger Logger = Logmanager.getlogger ();  @Override public void Setapplicationcontext (ApplicationContext ctx) throws Beansexception {Logger.info ("1 =    Startuplistener.setapplicationcontext "); } @Override public void Setservletcontext (ServletContext context) {Logger.info ("2 = startuplistener.sets    Ervletcontext "); } @Override public void Afterpropertiesset () throws Exception {Logger.info ("3 = Startuplistener.afterpropertiesset"); } @Override public void Onapplicationevent (Contextrefreshedevent evt) {logger.info ("4.1 = myapplicationl        Istener.onapplicationevent "); if (Evt.getapplicationcontext (). getParent () = = null) {Logger.info ("4.2 = Myapplicationlistener.onapplicati        OnEvent "); }    }}

At run time, the output is in the following order:

1 = Startuplistener.setapplicationcontext
2 = Startuplistener.setservletcontext
3 = Startuplistener.afterpropertiesset
4.1 = Myapplicationlistener.onapplicationevent
4.2 = Myapplicationlistener.onapplicationevent
4.1 = Myapplicationlistener.onapplicationevent

Note: The Onapplicationevent method fires multiple times, and the sooner the better it is, the more recommended to be handled in the Setapplicationcontext method.

How to enable a Spring MVC Web app to perform specific processing when it starts

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.