【ApplicationListener】Springboot各種事件監聽器

Springboot中SpringApplicationEvent的種類

以下圖:
html

主要包括6種:java

  1. ApplicationEnvironmentPreparedListener
  2. ApplicationFailedListener
  3. ApplicationPreparedListener
  4. ApplicationReadyListener
  5. ApplicationStartingListener
  6. SpringApplicationListener

各監聽器的啓動順序

以下是debug輸出結果:web

// 省略了部分
Connected to the target VM, address: '127.0.0.1:52000', transport: 'socket'
2017-09-28 16:53:27.273  INFO 6960 --- [           main] c.s.listeners.SpringApplicationListener  : 監聽器事件 【SpringApplicationEvent】
2017-09-28 16:53:27.277  INFO 6960 --- [           main] l.ApplicationEnvironmentPreparedListener : 監聽器事件 【ApplicationEnvironmentPreparedEvent】

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.7.RELEASE)

2017-09-28 16:53:27.359  INFO 6960 --- [           main] c.s.ApplicationlistenerApplication       : Starting ApplicationlistenerApplication on LIULIN-PC with PID 6960 (D:\workspace\SpringBootStudy\applicationlistener\target\classes started by liulin in D:\workspace\SpringBootStudy)
2017-09-28 16:53:27.359  INFO 6960 --- [           main] c.s.ApplicationlistenerApplication       : No active profile set, falling back to default profiles: default
2017-09-28 16:53:27.403  INFO 6960 --- [           main] c.s.l.ApplicationPreparedListener        : 監聽器事件 【ApplicationPreparedEvent】
2017-09-28 16:53:27.403  INFO 6960 --- [           main] c.s.listeners.SpringApplicationListener  : 監聽器事件 【SpringApplicationEvent】
2017-09-28 16:53:27.406  INFO 6960 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2f465398: startup date [Thu Sep 28 16:53:27 CST 2017]; root of context hierarchy
2017-09-28 16:53:28.878  INFO 6960 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-28 16:53:28.887  INFO 6960 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-09-28 16:53:28.888  INFO 6960 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.20
2017-09-28 16:53:28.952  INFO 6960 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-09-28 16:53:28.952  INFO 6960 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1549 ms
2017-09-28 16:53:29.070  INFO 6960 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-28 16:53:29.074  INFO 6960 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-28 16:53:29.074  INFO 6960 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-28 16:53:29.074  INFO 6960 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-28 16:53:29.074  INFO 6960 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-28 16:53:29.105  INFO 6960 --- [           main] com.ssslinppp.service.DemoServiceA       : 初始化實例:DemoServiceA
2017-09-28 16:53:29.107  INFO 6960 --- [           main] com.ssslinppp.service.DemoServiceB       : 初始化實例:DemoServiceB
2017-09-28 16:53:29.327  INFO 6960 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2f465398: startup date [Thu Sep 28 16:53:27 CST 2017]; root of context hierarchy
2017-09-28 16:53:29.382  INFO 6960 --- [           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.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-09-28 16:53:29.382  INFO 6960 --- [           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.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-09-28 16:53:29.420  INFO 6960 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-28 16:53:29.420  INFO 6960 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-28 16:53:29.451  INFO 6960 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-28 16:53:29.626  INFO 6960 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-09-28 16:53:29.668  INFO 6960 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-09-28 16:53:29.671  INFO 6960 --- [           main] c.s.listeners.ApplicationReadyListener   : 監聽器事件 【ApplicationReadyEvent】 : ApplicationReadyListener
2017-09-28 16:53:29.671  INFO 6960 --- [           main] c.s.listeners.SpringApplicationListener  : 監聽器事件 【SpringApplicationEvent】
2017-09-28 16:53:29.672  INFO 6960 --- [           main] c.s.ApplicationlistenerApplication       : Started ApplicationlistenerApplication in 2.577 seconds (JVM running for 3.022)

從上面能夠看出,順序以下:spring

  1. SpringApplicationEvent
  2. ApplicationEnvironmentPreparedEvent :能夠經過ConfigurableEnvironment實例對象來修改 以及 獲取默認的環境信息。
  3. 初始化圖標(banner)
  4. ApplicationPreparedEvent :這個時候spring容器中的bean尚未被徹底的加載,若是經過ConfigurableApplicationContext獲取bean會報錯的
  5. SpringApplicationEvent
  6. 映射基本的servlet URL: [/][/*]
  7. 初始化Spring容器中的beans
  8. ApplicationReadyEvent :能夠經過ApplicationReadyEvent獲取ConfigurableApplicationContext,而後經過ConfigurableApplicationContext 獲取bean的信息

各類SpringApplicationEvent的主要用途示例

相關文章
相關標籤/搜索