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