In some cases, we need to perform some operations after the Spring Boot container starts and loads. At this time, we can implement the ApplicationListener<E extends ApplicationEvent> interface and specify the corresponding event to perform operations, such as starting some custom daemon threads.
ApplicationContextEvent is an event base class raised by ApplicationContext. It has several implementation classes:
ContextRefreshedEvent : The event is triggered when the ApplicationContext container is initialized or refreshed and executed once
ContextStartedEvent : This event is triggered when the ApplicationContext container is started using the start() method of the ConfigurableApplicationContext interface
ContextClosedEvent : This event is triggered when the close() method of the ConfigurableApplicationContext interface is closed.
ContextStopedEvent : This event is triggered when the stop() method of the ConfigurableApplicationContext interface is stopped.
Code example
@Componentpublic class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("This event is triggered when container initialization or refreshing is executed once"); }}Summarize
The above is what the editor introduces to you to perform specific operations when loading the Spring Boot container. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!