Spring Boot 2.0 新特性ApplicationStartedEvent實戰
一 點睛
在Spring Boot 2.0中對事件模型做了一些增強,主要就是增加了ApplicationStartedEvent事件,所以在2.0版本中所有的事件按執行的先後順序如下:
-
ApplicationStartingEvent
-
ApplicationEnvironmentPreparedEvent
-
ApplicationPreparedEvent
-
ApplicationStartedEvent <= 新增的事件
-
ApplicationReadyEvent
-
ApplicationFailedEvent
二 實戰
1 新增依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies>
2 ApplicationEnvironmentPreparedEventListener監聽器
package com.didispace; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.context.ApplicationListener; @Slf4j public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { log.info("......ApplicationEnvironmentPreparedEvent......"); } }
3 ApplicationFailedEventListener監聽器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
log.info("......ApplicationFailedEvent......");
}
}
4 ApplicationPreparedEventListener監聽器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
log.info("......ApplicationPreparedEvent......");
}
}
5 ApplicationReadyEventListener監聽器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("......ApplicationReadyEvent......");
}
}
6 ApplicationStartedEventListener監聽器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
log.info("......ApplicationStartedEvent......");
}
}
7 ApplicationStartingEventListener監聽器
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
@Slf4j
public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
log.info("......ApplicationStartingEvent......");
}
}
8 啟動類
1 程式碼
package com.didispace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@Slf4j
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public DataLoader dataLoader() {
return new DataLoader();
}
@Slf4j
static class DataLoader implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
log.info("Loading data...");
}
}
}
2 說明
官方文件對ApplicationStartedEvent和ApplicationReadyEvent的解釋:
An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests
從文件中我們可以知道他們兩中間還有一個過程就是command-line runners被呼叫的內容。所以,為了更準確的感受這兩個事件的區別,我們在應用主類中加入CommandLineRunner的實現。
三 執行結果
2018-10-24 19:32:01.772 INFO 14708 --- [ main] c.d.ApplicationPreparedEventListener : ......ApplicationPreparedEvent......
......
2018-10-24 19:32:05.032 INFO 14708 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-10-24 19:32:05.068 INFO 14708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-24 19:32:05.072 INFO 14708 --- [ main] com.didispace.Application : Started Application in 3.8 seconds (JVM running for 4.372)
2018-10-24 19:32:05.074 INFO 14708 --- [ main] c.d.ApplicationStartedEventListener : ......ApplicationStartedEvent......
2018-10-24 19:32:05.075 INFO 14708 --- [ main] com.didispace.Application$DataLoader : Loading data...
2018-10-24 19:32:05.076 INFO 14708 --- [ main] c.d.ApplicationReadyEventListener : ......ApplicationReadyEvent......
四 知識拾遺
1 關於@Slf4j註解
<!--引入日誌 @Slf4j註解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然後在類上寫上@Slf4j註解
在方法中直接使用,就可以列印日誌。
五 參考
https://blog.csdn.net/qq_26525215/article/details/79182628
http://blog.didispace.com/Spring-Boot-2-0-feature-2-ApplicationStartedEvent/