usage Scenario: in a real project, we usually want the Web application to start (this is the spring IOC container load bean) After the success of the initialization work, such as: Database loading, a specific task check and so on.
implementation Process : Define a Myapplicationlistener class to implement the Applicationlistener<applicationevent> interface. This class is typically executed three times if the configuration file (Springmvc.xml, etc.) is used. To solve this problem, I defined a variable isstart, and the default initial value is False.
The code is as follows:
@Controllerpublic class Myapplicationlistener implements applicationlistener<applicationevent> {The variable is defined primarily to prevent the business code from being executed three times,private static Boolean Isstart = false;@Overridepublic void Onapplicationevent (Applicationevent event) { if (!isstart) { Business code for a specific task SYSTEM.OUT.PRINTLN ("Initialize work here------"); Isstart = true; }}}
Talking about the use of Spring Applicationlistener listener