Spring triggers contextrefreshedevent events via Applicationlistener interface
At development time, it is sometimes necessary to execute specific code when the entire application starts running, such as initializing the environment, preparing the test data, loading some data into memory, and so on.
Applicationlistener can be implemented in spring to trigger the Contextrefreshedevent event (up and down file refresh events) when loading is complete
Example: Timed Tasks
The above resolves a problem:
There is a problem in the Web project (Spring MVC), the system will have two containers, one is the root application context, and the other is our own Projectname-servlet context (as root Application the child container of the context).
In this case, it causes the Onapplicationevent method to be executed two times. To avoid the above mentioned problems, you can call the logic code only after the root application context initialization is complete, and the other container initialization is done without any processing, the modified code
Spring to trigger Contextrefreshedevent event via Applicationlistener