1. 程式人生 > >Spring Boot的事件和監聽

Spring Boot的事件和監聽

在Spring Boot的框架中,處理一些通用的Spring框架的事件(如ContextRefreshedEvent),在SpringApplication中也會發送一下其他的應用的事件,下面來聊聊這些事件的觸發時間和用法。

在程式的啟動中,會有以下的事件觸發【在2.0版本中所有的事件按執行的先後順序如下】

1、ApplicationStartingEvent:開始啟動,但在除了註冊監聽器和初始化程式之外的任何處理之前。

2、ApplicationEnvironmentPreparedEvent:spring boot 對應Enviroment已經準備完畢,但此時上下文context還沒有建立。

3、ApplicationPreparedEvent:spring boot上下文context建立完成,但此時spring中的bean是沒有完全載入完成的。

4、ApplicationStartedEvent:在上下文建立之後但在任何應用程式和命令列引數被呼叫之前傳送

5、ApplicationReadyEvent:所有都準備完成

6、ApplicationFailedEvent:spring boot啟動異常時執行事件

 

由於有一些事件會在ApplicationContext之前就建立了,所已不能使用@Bean來註冊事件。事件的註冊有以下兩種方法:

1、使用SpringApplication.addListeners(…​)或者SpringApplicationBuilder.listeners(…​)註冊

2、新增一個META-INF/spring.factories檔案,然後在了按照以下格式新增要註冊的事件:org.springframework.context.ApplicationListener=com.example.project.MyListener

 

下面來看事件DEMO程式碼

專案的整體結構:

 

1、MyApplicationEnvironmentPreparedEvent.java

public class MyApplicationEnvironmentPreparedEvent implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {

LOGGER.info("ApplicationEnvironmentPreparedEvent...");

}

}

 

2、MyApplicationFailedEvent.java

public class MyApplicationFailedEvent implements ApplicationListener<ApplicationFailedEvent> {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void onApplicationEvent(ApplicationFailedEvent applicationFailedEvent) {

LOGGER.info("ApplicationFailedEvent...");

}

}

 

3、MyApplicationPreparedEvent .java

public class MyApplicationPreparedEvent implements ApplicationListener<ApplicationPreparedEvent> {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent) {

LOGGER.info("ApplicationPreparedEvent...");

}

}

 

4、MyApplicationReadyEvent .java

public class MyApplicationReadyEvent implements ApplicationListener<ApplicationReadyEvent> {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {

LOGGER.info("ApplicationReadyEvent...");

}

}

 

5、MyApplicationStartedEvent .java

public class MyApplicationStartedEvent implements ApplicationListener<ApplicationStartedEvent> {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {

LOGGER.info("ApplicationStartedEvent...");

}

}

 

6、MyApplicationStartingEvent .java

public class MyApplicationStartingEvent implements ApplicationListener<ApplicationStartingEvent> {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void onApplicationEvent(ApplicationStartingEvent applicationStartingEvent) {

LOGGER.info("ApplicationStartingEvent...");

}

}

 

7、StartUpRunner.java

@Component

public class StartUpRunner implements CommandLineRunner {

 

private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

 

@Override

public void run(String... strings) throws Exception {

LOGGER.info("StartUPRunner ....");

}

}

//方法1:使用SpringApplication.addListeners(…​),則其Application .java檔案為【此時不用spring.factories檔案】

7、Application .java

@SpringBootApplication

public class Application {

 

public static void main(String[] args) {

SpringApplication springBootApplication = new SpringApplication(Application.class);

 

springBootApplication.addListeners(new MyApplicationEnvironmentPreparedEvent());

springBootApplication.addListeners(new MyApplicationFailedEvent());

springBootApplication.addListeners(new MyApplicationPreparedEvent());

springBootApplication.addListeners(new MyApplicationReadyEvent());

springBootApplication.addListeners(new MyApplicationStartedEvent());

springBootApplication.addListeners(new MyApplicationStartingEvent());

springBootApplication.run(args);

}

}

 

 

方法2:新增一個META-INF/spring.factories檔案註冊

,則其Application .java檔案為

7、Application .java

@SpringBootApplication

public class Application {

 

public static void main(String[] args) {

SpringApplication springBootApplication = new SpringApplication(Application.class);

 

springBootApplication.run(args);

}

}

 

spring.factories檔案為:

org.springframework.context.ApplicationListener=com.example.demo.MyApplicationEnvironmentPreparedEvent\

,com.example.demo.MyApplicationFailedEvent\

,com.example.demo.MyApplicationPreparedEvent\

,com.example.demo.MyApplicationReadyEvent\

,com.example.demo.MyApplicationStartedEvent\

,com.example.demo.MyApplicationStartingEvent

 

執行後的結果為:

原始碼地址:https://github.com/cdy1263/SpringBootDemo/tree/master/Chapter_2_1