自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Boot 2.0 新特性(二):新增事件ApplicationStartedEvent

開(kāi)發(fā) 開(kāi)發(fā)工具
今天繼續(xù)來(lái)聊Spring Boot 2.0的新特性。本文將具體說(shuō)說(shuō)2.0版本中的事件模型,尤其是新增的事件:ApplicationStartedEvent。

今天繼續(xù)來(lái)聊Spring Boot 2.0的新特性。本文將具體說(shuō)說(shuō)2.0版本中的事件模型,尤其是新增的事件:ApplicationStartedEvent。

[[231935]]

在Spring Boot 2.0中對(duì)事件模型做了一些增強(qiáng),主要就是增加了ApplicationStartedEvent事件,所以在2.0版本中所有的事件按執(zhí)行的先后順序如下:

  • ApplicationStartingEvent
  • ApplicationEnvironmentPreparedEvent
  • ApplicationPreparedEvent
  • ApplicationStartedEvent <= 新增的事件
  • ApplicationReadyEvent
  • ApplicationFailedEvent

從上面的列表中,我們可以看到ApplicationStartedEvent位于ApplicationPreparedEvent之后,ApplicationReadyEvent之前。

下面我們通過(guò)代碼的方式來(lái)直觀的感受這個(gè)事件的切入位置,以便與將來(lái)我們?cè)谶@個(gè)切入點(diǎn)加入自己需要的邏輯。

***步:我們可以編寫(xiě)ApplicationPreparedEvent、ApplicationStartedEvent以及ApplicationReadyEvent三個(gè)事件的監(jiān)聽(tīng)器,然后在這三個(gè)事件觸發(fā)的時(shí)候打印一些日志來(lái)觀察它們各自的切入點(diǎn),比如:

  1. @Slf4j 
  2. public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> { 
  3.  
  4.     @Override 
  5.     public void onApplicationEvent(ApplicationPreparedEvent event) { 
  6.         log.info("......ApplicationPreparedEvent......"); 
  7.     } 
  8.  
  9.  
  10. @Slf4j 
  11. public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> { 
  12.  
  13.     @Override 
  14.     public void onApplicationEvent(ApplicationStartedEvent event) { 
  15.         log.info("......ApplicationStartedEvent......"); 
  16.     } 
  17.  
  18.  
  19. @Slf4j 
  20. public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> { 
  21.  
  22.     @Override 
  23.     public void onApplicationEvent(ApplicationReadyEvent event) { 
  24.         log.info("......ApplicationReadyEvent......"); 
  25.     } 
  26.  

第二步:在/src/main/resources/目錄下新建:META-INF/spring.factories配置文件,通過(guò)配置org.springframework.context.ApplicationListener來(lái)加載上面我們編寫(xiě)的監(jiān)聽(tīng)器。

  1. org.springframework.context.ApplicationListener= 
  2.   com.didispace.ApplicationPreparedEventListener,\ 
  3.   com.didispace.ApplicationReadyEventListener,\ 
  4.   com.didispace.ApplicationStartedEventListener 

此時(shí),我們運(yùn)行Spring Boot應(yīng)用可以獲得類(lèi)似如下日志輸出:

  1. 2018-03-07 18:15:18.591  INFO 83387 --- [           main] com.didispace.Application                : Starting Application on zhaiyongchaodeMacBook-Pro.local with PID 83387 (/Users/zhaiyongchao/Documents/git/github/SpringBoot-Learning/Chapter1-2-1/target/classes started by zhaiyongchao in /Users/zhaiyongchao/Documents/git/github/SpringBoot-Learning/Chapter1-2-1) 
  2. 2018-03-07 18:15:18.591  INFO 83387 --- [           main] com.didispace.Application                : No active profile set, falling back to default profiles: default 
  3. 2018-03-07 18:15:18.658  INFO 83387 --- [           main] c.d.ApplicationPreparedEventListener     : ......ApplicationPreparedEvent...... 
  4. 2018-03-07 18:15:18.662  INFO 83387 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@20d3d15a: startup date [Wed Mar 07 18:15:18 CST 2018]; root of context hierarchy 
  5. 2018-03-07 18:15:19.879  INFO 83387 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http) 
  6. 2018-03-07 18:15:19.926  INFO 83387 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat] 
  7. 2018-03-07 18:15:19.930  INFO 83387 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28 
  8. 2018-03-07 18:15:19.946  INFO 83387 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/zhaiyongchao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 
  9. 2018-03-07 18:15:20.068  INFO 83387 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 
  10. 2018-03-07 18:15:20.068  INFO 83387 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1410 ms 
  11. 2018-03-07 18:15:20.210  INFO 83387 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/] 
  12. 2018-03-07 18:15:20.214  INFO 83387 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*] 
  13. 2018-03-07 18:15:20.214  INFO 83387 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
  14. 2018-03-07 18:15:20.214  INFO 83387 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*] 
  15. 2018-03-07 18:15:20.215  INFO 83387 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*] 
  16. 2018-03-07 18:15:20.513  INFO 83387 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@20d3d15a: startup date [Wed Mar 07 18:15:18 CST 2018]; root of context hierarchy 
  17. 2018-03-07 18:15:20.592  INFO 83387 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 
  18. 2018-03-07 18:15:20.593  INFO 83387 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 
  19. 2018-03-07 18:15:20.623  INFO 83387 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
  20. 2018-03-07 18:15:20.623  INFO 83387 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
  21. 2018-03-07 18:15:20.660  INFO 83387 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
  22. 2018-03-07 18:15:20.787  INFO 83387 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 
  23. 2018-03-07 18:15:20.839  INFO 83387 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '' 
  24. 2018-03-07 18:15:20.843  INFO 83387 --- [           main] com.didispace.Application                : Started Application in 2.866 seconds (JVM running for 3.337) 
  25. 2018-03-07 18:15:20.845  INFO 83387 --- [           main] c.d.ApplicationStartedEventListener      : ......ApplicationStartedEvent...... 
  26. 2018-03-07 18:15:20.847  INFO 83387 --- [           main] c.d.ApplicationReadyEventListener        : ......ApplicationReadyEvent...... 

從日志中我們可以看到清晰的看到ApplicationPreparedEvent、ApplicationStartedEvent以及ApplicationReadyEvent三個(gè)事件的切入點(diǎn)。通過(guò)這個(gè)例子可能讀者會(huì)感到疑問(wèn):ApplicationStartedEvent和ApplicationReadyEvent從事件命名和日志輸出位置來(lái)看,都是應(yīng)用加載完成之后的事件,它們是否有什么區(qū)別呢?

下面可以看看官方文檔對(duì)ApplicationStartedEvent和ApplicationReadyEvent的解釋?zhuān)?/p>

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

從文檔中我們可以知道他們兩中間還有一個(gè)過(guò)程就是command-line runners被調(diào)用的內(nèi)容。所以,為了更準(zhǔn)確的感受這兩個(gè)事件的區(qū)別,我們?cè)趹?yīng)用主類(lèi)中加入CommandLineRunner的實(shí)現(xiàn),比如:

  1. @Slf4j 
  2. @SpringBootApplication 
  3. public class Application { 
  4.  
  5.     public static void main(String[] args) { 
  6.         SpringApplication.run(Application.class, args); 
  7.     } 
  8.  
  9.     @Bean 
  10.     public DataLoader dataLoader() { 
  11.         return new DataLoader(); 
  12.     } 
  13.  
  14.     @Slf4j 
  15.     static class DataLoader implements CommandLineRunner { 
  16.  
  17.         @Override 
  18.         public void run(String... strings) throws Exception { 
  19.             log.info("Loading data..."); 
  20.         } 
  21.     } 
  22.  

***,我們?cè)龠\(yùn)行程序,此時(shí)我們可以發(fā)現(xiàn)這兩個(gè)事件中間輸出了上面定義的DataLoader的輸出內(nèi)容,具體如下:

  1. 2018-03-07 18:15:20.845  INFO 83387 --- [main] c.d.ApplicationStartedEventListener      : ......ApplicationStartedEvent...... 
  2. 2018-03-07 18:15:20.846  INFO 83387 --- [main] com.didispace.Application$DataLoader     : Loading data... 
  3. 2018-03-07 18:15:20.847  INFO 83387 --- [main] c.d.ApplicationReadyEventListener        : ......ApplicationReadyEvent...... 

代碼示例

本文的相關(guān)例子可以查看下面?zhèn)}庫(kù)中的Chapter1-2-1目錄:

Github:https://github.com/dyc87112/SpringBoot-Learning

Gitee:https://gitee.com/didispace/SpringBoot-Learning

【本文為51CTO專(zhuān)欄作者“翟永超”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)通過(guò)51CTO聯(lián)系作者獲取授權(quán)】

戳這里,看該作者更多好文

 

責(zé)任編輯:武曉燕 來(lái)源: 51CTO專(zhuān)欄
相關(guān)推薦

2018-05-30 15:10:24

Spring BootList類(lèi)型

2009-06-15 16:15:37

Spring2.0新特

2009-06-18 15:40:07

Spring Batc

2013-02-25 14:02:07

RubyWeb

2012-03-14 12:29:55

JavaPlay Framwo

2022-10-26 07:14:25

Spring 6Spring業(yè)務(wù)

2025-04-16 10:03:40

開(kāi)發(fā)Spring應(yīng)用程序

2009-07-30 14:55:43

ASP.NET 2.0

2024-05-31 14:06:55

SpringCDSGraalVM

2025-04-29 07:44:26

配置校驗(yàn)機(jī)制

2009-06-24 09:22:04

Spring2.5新特

2009-09-08 11:26:35

Spring 3.0

2024-10-11 11:32:22

Spring6RSocket服務(wù)

2009-07-31 09:29:05

GWT 2.0

2012-11-16 11:11:06

深度影音Linux Deepi

2009-11-04 14:17:34

ADO.NET 2.0

2011-09-30 14:15:10

Sencha ToucSencha Touc

2012-07-02 10:43:49

JVMGroovyJava

2013-02-26 09:36:57

RubyRuby 2.0

2018-06-20 15:33:44

Spring BootJava 9JDK
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)