一、自定義監聽器:
1、創建:
META-INF/spring.factories
2、添加:
org.springframework.context.ApplicationListener=com.example.project.MyListener
二、應用程序事件發送順序如下
Application events are sent in the following order, as your application runs:
1、An ApplicationStartedEvent is sent at the start of a run, but before any processing except the registration of listeners and initializers.
ApplicationStartedEvent在任何處理之前,程序開始運行時被發送,初始化和自定義注冊監聽事件除外
2、An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.
ApplicationEnvironmentPreparedEvent在上下文被創建之前,應用環境被已知的上下文環境中使用時被發送
3、An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.
ApplicationPreparedEvent在刷新開始之前,beans加載之后被發送
4、An ApplicationReadyEvent is sent after the refresh and any related callbacks have been processed to indicate the application is ready to service requests.
ApplicationReadyEvent在刷新后被發送,并且任何相關的回調都已經被處理,表明該應用程序已經準備好處理服務請求
5、An ApplicationFailedEvent is sent if there is an exception on startup.
ApplicationFailedEvent如果啟動時存在異常時被發送
You often won’t need to use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks.
我們不需要使用應用程序事件,但是很方便的知道他們存在,在SpringBoot內部使用各種事件來處理各種任務