栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot Application事件监听

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot Application事件监听

先说结论

SpringBoot Application共支持6种事件监听,按顺序分别是:

  1. ApplicationStartingEvent:在Spring最开始启动的时候触发
  2. ApplicationEnvironmentPreparedEvent:在Spring已经准备好上下文但是上下文尚未创建的时候触发
  3. ApplicationPreparedEvent:在Bean定义加载之后、刷新上下文之前触发
  4. ApplicationStartedEvent:在刷新上下文之后、调用application命令之前触发
  5. ApplicationReadyEvent:在调用applicaiton命令之后触发
  6. ApplicationFailedEvent:在启动Spring发生异常时触发

另外:

  • ApplicationRunner和CommandLineRunner的执行在第五步和第六步之间
  • Bean的创建在第三步和第四步之间
  • 在启动类中,执行SpringApplication.run()方法后的代码,会在第六步后执行
再上代码:

ApplicationStartingEvent

public class ApplicationStartingEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationStartingEvent applicationStartingEvent) {
 System.out.println("============>>>>> applicationStartingEvent is trigged");
 System.out.println(applicationStartingEvent.getTimestamp());
 System.out.println("============>>>>> End");
    }
}

ApplicationEnvironmentPreparedEvent

public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
 System.out.println("============>>>>> ApplicationEnvironmentPreparedEvent is trigged");
 System.out.println(applicationEnvironmentPreparedEvent.getTimestamp());
 System.out.println("============>>>>> End");
    }
}

ApplicationEnvironmentPreparedEvent

ApplicationPreparedEvent

public class ApplicationPreparedEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent) {
 System.out.println("============>>>>> applicationPreparedEvent is trigged");
 System.out.println(applicationPreparedEvent.getTimestamp());
 System.out.println("============>>>>> End");
    }
}

ApplicationStartedEvent

public class ApplicationStartedEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
 System.out.println("============>>>>> applicationStartedEvent is trigged");
 System.out.println(applicationStartedEvent.getTimestamp());
 System.out.println("============>>>>> End");
    }
}

ApplicationReadyEvent

public class ApplicationReadyEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
 System.out.println("============>>>>> applicationReadyEvent is trigged");
 System.out.println(applicationReadyEvent.getTimestamp());
 System.out.println("============>>>>> End");
    }
}

ApplicationFailedEvent

public class ApplicationFailedEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationFailedEvent applicationFailedEvent) {
 System.out.println("============>>>>> ApplicationFailedEvent is trigged");
 System.out.println(applicationFailedEvent.getTimestamp());
 System.out.println("============>>>>> End");
    }
}

主启动类

@SpringBootApplication
public class SpringBootTestApplication {

    public static void main(String[] args) {
 SpringApplication springApplication = new SpringApplication(SpringBootTestApplication.class);
 springApplication.addListeners(new ApplicationEnvironmentPreparedEventListener());
 springApplication.addListeners(new ApplicationFailedEventListener());
 springApplication.addListeners(new ApplicationPreparedEventListener());
 springApplication.addListeners(new ApplicationReadyEventListener());
 springApplication.addListeners(new ApplicationStartedEventListener());
 springApplication.addListeners(new ApplicationStartingEventListener());
 springApplication.run(args);
    }
}

运行结果

Connected to the target VM, address: '127.0.0.1:62927', transport: 'socket'
============>>>>> applicationStartingEvent is trigged
============>>>>> End
============>>>>> ApplicationEnvironmentPreparedEvent is trigged
============>>>>> End

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

2018-11-01 14:52:35.117  INFO 2044 --- [    main] c.l.s.b.test.SpringBootTestApplication   : Starting SpringBootTestApplication on EDIANZU-ETGVGB5 with PID 2044 (D:CodeSelfCodeSpringCloudTestSpringBootTesttargetclasses started by Administrator in D:CodeSelfCodeSpringCloud)
2018-11-01 14:52:35.122  INFO 2044 --- [    main] c.l.s.b.test.SpringBootTestApplication   : No active profile set, falling back to default profiles: default
============>>>>> applicationPreparedEvent is trigged
============>>>>> End
2018-11-01 14:52:35.212  INFO 2044 --- [    main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3406472c: startup date [Thu Nov 01 14:52:35 CST 2018]; root of context hierarchy
2018-11-01 14:52:36.891  INFO 2044 --- [    main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-11-01 14:52:36.920  INFO 2044 --- [    main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-11-01 14:52:36.920  INFO 2044 --- [    main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-11-01 14:52:36.925  INFO 2044 --- [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: [D:Program FilesJavajdk1.8.0_191bin;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:Program Files (x86)Common FilesOracleJavajavapath;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program FilesIntelWiFibin;C:Program FilesCommon FilesIntelWirelessCommon;C:Program FilesThinkPadBluetooth Software;C:Program FilesThinkPadBluetooth Softwaresyswow64;C:Program Files (x86)IntelOpenCL SDK2.0binx86;C:Program Files (x86)IntelOpenCL SDK2.0binx64;D:Program FilesJavajdk1.8.0_191bin;D:Program FilesGitcmd;D:Program Filesapache-maven-3.5.4bin;C:Program FilesIntelWiFibin;C:Program FilesCommon FilesIntelWirelessCommon;.]
2018-11-01 14:52:37.048  INFO 2044 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext
2018-11-01 14:52:37.048  INFO 2044 --- [ost-startStop-1] o.s.web.context.ContextLoader     : Root WebApplicationContext: initialization completed in 1841 ms
2018-11-01 14:52:37.377  INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-11-01 14:52:37.389  INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 14:52:37.857  INFO 2044 --- [    main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3406472c: startup date [Thu Nov 01 14:52:35 CST 2018]; root of context hierarchy
2018-11-01 14:52:37.922  INFO 2044 --- [    main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-11-01 14:52:37.923  INFO 2044 --- [    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)
2018-11-01 14:52:37.973  INFO 2044 --- [    main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 14:52:37.973  INFO 2044 --- [    main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 14:52:38.160  INFO 2044 --- [    main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-11-01 14:52:38.206  INFO 2044 --- [    main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-01 14:52:38.212  INFO 2044 --- [    main] c.l.s.b.test.SpringBootTestApplication   : Started SpringBootTestApplication in 3.976 seconds (JVM running for 5.088)
============>>>>> applicationStartedEvent is trigged
============>>>>> End
============>>>>> applicationReadyEvent is trigged
============>>>>> End
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号