一、自定義監(jiān)聽(tīng)器:
1佑颇、創(chuàng)建:
META-INF/spring.factories
2空民、添加:
org.springframework.context.ApplicationListener=com.example.project.MyListener
二、應(yīng)用程序事件發(fā)送順序如下
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在任何處理之前,程序開(kāi)始運(yùn)行時(shí)被發(fā)送挡育,初始化和自定義注冊(cè)監(jiān)聽(tīng)事件除外
2、An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.
ApplicationEnvironmentPreparedEvent在上下文被創(chuàng)建之前朴爬,應(yīng)用環(huán)境被已知的上下文環(huán)境中使用時(shí)被發(fā)送
3即寒、An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.
ApplicationPreparedEvent在刷新開(kāi)始之前,beans加載之后被發(fā)送
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在刷新后被發(fā)送母赵,并且任何相關(guān)的回調(diào)都已經(jīng)被處理,表明該應(yīng)用程序已經(jīng)準(zhǔn)備好處理服務(wù)請(qǐng)求
5具滴、An ApplicationFailedEvent is sent if there is an exception on startup.
ApplicationFailedEvent如果啟動(dòng)時(shí)存在異常時(shí)被發(fā)送
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.
我們不需要使用應(yīng)用程序事件凹嘲,但是很方便的知道他們存在,在SpringBoot內(nèi)部使用各種事件來(lái)處理各種任務(wù)