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()方法后的代码,会在第六步后执行

多说无益,直接上代码:

public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {

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

ApplicationStartingEvent

public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

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

ApplicationEnvironmentPreparedEvent

public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {

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

ApplicationPreparedEvent

public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {

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

ApplicationStartedEvent

public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

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

ApplicationReadyEvent

public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {

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

ApplicationFailedEvent

@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
1541055154248
============>>>>> End
============>>>>> ApplicationEnvironmentPreparedEvent is trigged
1541055154320
============
>>>>> 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:\Code\SelfCode\SpringCloud\Test\SpringBootTest\target\classes started by Administrator in D:\Code\SelfCode\SpringCloud)
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
1541055155205
============>>>>> 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 Files\Java\jdk1.8.0_191\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\ThinkPad\Bluetooth Software\;C:\Program Files\ThinkPad\Bluetooth Software\syswow64;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;D:\Program Files\Java\jdk1.8.0_191\bin;D:\Program Files\Git\cmd;D:\Program Files\apache-maven-3.5.4\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;.]
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: [/*]
2018-11-01 14:52:37.390 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-01 14:52:37.390 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-11-01 14:52:37.390 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-01 14:52:37.598 INFO 2044 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/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<java.util.Map<java.lang.String, java.lang.Object>> 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
1541055158213
============>>>>> End
============>>>>> applicationReadyEvent is trigged
1541055158214
============>>>>> End

SpringBoot Application事件监听的更多相关文章

  1. SpringBoot的事件监听

    事件监听的流程分为三步:1.自定义事件,一般是继承ApplicationEvent抽象类.2.定义事件监听器,一般是实现ApplicationListener接口.3.a.启动的时候,需要将监听器加入 ...

  2. springboot 中事件监听模型的一种实现

    目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...

  3. springboot~ EventListener事件监听的使用

    EventListener事件触发和监听器可以对代码解耦,在一些与业务无关的,通用的操作方法,我们可以把它设计成事件监听器,像通知,消息这些模块都可以这样设计. 事件源 @Getter @Builde ...

  4. SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)

    SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...

  5. SpringBoot事件监听机制及观察者模式/发布订阅模式

    目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...

  6. spring boot 源码赏析之事件监听

    使用spring Boot已经快1年多了,期间一直想点开springboot源码查看,但由于种种原因一直未能如愿(主要是人类的惰性...),今天就拿springboot 的监听事件祭刀. spring ...

  7. SpringBoot入门之事件监听

    spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种: ApplicationStartingEvent Applicatio ...

  8. SpringBoot框架(6)--事件监听

    一.场景:类与类之间的消息通信,例如创建一个对象前后做拦截,日志等等相应的事件处理. 二.事件监听步骤 (1)自定义事件继承ApplicationEvent抽象类 (2)自定义事件监听器,一般实现Ap ...

  9. springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署

    知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...

随机推荐

  1. ajax 'Content-Type': 'multipart/form-data' ->文件上传

    'Content-Type': 'multipart/form-data' :指定传输数据为二进制数据,例如图片.mp3.文件

  2. pyhon 内置函数

    chr()   asci码 dir()  目录,显示目录. divmod(10,3)   返回商和余数   例如  (3, 1)   返回的为一个元组    可以用于分页 enumerate()    ...

  3. 小程序canvas 变换

    var ctx = wx.createCanvasContext('base'); var centerX = 375/ 2; var centerY = 200; var rotate = 90; ...

  4. spring mvc 的@PathVariable对应中文乱码解决办法

    参考:https://www.aliyun.com/jiaocheng/800577.html

  5. windows驱动不要签名

    BCDEDIT -SET LOADOPTIONS DISABLE_INTEGRITY_CHECKSBCDEDIT -SET TESTSIGNING ON

  6. 使用PYaudio录制音频和视频(自己)

    参考:https://blog.csdn.net/zhaoyun_zzz/article/details/84341801 音频录制:简洁版 import pyaudioimport waveimpo ...

  7. R 再也不用愁变量太多跑回归太麻烦!R语言循环常用方法总结

    在高维数据分析过程中,为了筛选出与目标结局相关的变量,通常会用到回归分析,但是因为自变量较多,往往要进行多次回归.这就是统计编程语言发挥作用的时候了 有些大神们认为超过3次的复制粘贴就可以考虑使用循环 ...

  8. Java笔记: 初始化块

    Java语言提供了很多类初始化的方法,包括构造器.初始化器等.除了这两种方法之外,我们还可以用初始化块(initialization block)来实现初始化功能. 基本语法 初始化块是出现在类声明中 ...

  9. 谈谈函数式编程curry

    Curry概念 The concept is simple: You can call a function with fewer arguments than it expects. It retu ...

  10. Spring配置数据源以及hibernate

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...