Springboot中SpringApplicationEvent的种类

如下图:

主要包括6种:

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

各监听器的启动顺序

如下是debug输出结果:

// 省略了部分
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)

从上面可以看出,顺序如下:

  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的主要用途示例

【ApplicationListener】Springboot各类事件监听器的更多相关文章

  1. SpringBoot事件监听器源码分析

    本文涉及到Spring的监听器,如果不太了解请先阅读之前的Spring监听器的文章. SpringBoot事件监听器初始化 SpringBoot中默认定义了11个事件监听器对象,全部定义在META-I ...

  2. SpringBoot各类扩展点详解

    一.前言 上篇文章我们深入分析了SpringBoot的一站式启动流程.然后我们知道SpringBoot的主要功能都是依靠它内部很多的扩展点来完成的,那毋容置疑,这些扩展点是我们应该深入了解的,那么本次 ...

  3. Spring 框架中注释驱动的事件监听器详解

    事件交互已经成为很多应用程序不可或缺的一部分,Spring框架提供了一个完整的基础设施来处理瞬时事件.下面我们来看看Spring 4.2框架中基于注释驱动的事件监听器. 1.早期的方式 在早期,组件要 ...

  4. SpringBoot的事件监听

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

  5. spring 启动完成后事件监听器处理

    有时候我们在spring容器启动完成后,我们需要做一些处理动作,这个时候怎么做呢? spring提供了事件监听器的处理机制. spring提供了内置的几类的事件: ContextClosedEvent ...

  6. Spring 4.2框架中注释驱动的事件监听器详解

    事件交互已经成为很多应用程序不可或缺的一部分,spring框架提供了一个完整的基础设施来处理瞬时事件.下面我们来看看Spring 4.2框架中基于注释驱动的事件监听器. 1.早期的方式 在早期,组件要 ...

  7. java设计模式--观察者模式和事件监听器模式

    观察者模式 观察者模式又称为订阅—发布模式,在此模式中,一个目标对象管理所有相依于它的观察者对象,并且在它本身的状态改变时主动发出通知.这通常透过呼叫各观察者所提供的方法来实现.此种模式通常被用来事件 ...

  8. springBoot的事件机制---GenericApplicationListener用法

    springBoot的事件机制---GenericApplicationListener用法 什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环 ...

  9. passive 的事件监听器

    很久以前,addEventListener() 的参数约定是这样的: addEventListener(type, listener, useCapture) 后来,最后一个参数,也就是控制监听器是在 ...

随机推荐

  1. Oracle使用笔记(一)

    目录 一.创建表: 二.编辑表数据: 三.Oracle基本数据类型: (一) 字符串类型 1.1:CHAR类型 CHAR(size [BYTE | CHAR]) 1.2: NCHAR类型 1.3 VA ...

  2. 两个Inline-block元素不对齐

    可能是有一个元素用了overflow:hidden,(给另一个元素也加上) 也有可能是基线没对齐,用vertical-align:top可以

  3. vim 的编辑模式 命令模式

    1.vim的编辑模式    进入编辑模式 按键: a  i  o a: 表示在光标当前的,后面开始插入,写数据 i : 则表示 前面 . o : 表面在光标当前的,下一行开始写入数据. O : 大写的 ...

  4. 【湖南师范大学2018年大学生程序设计竞赛新生赛 L】【HDOJ2476】【区间DP】

    https://www.nowcoder.com/acm/contest/127/L L 小小粉刷匠 题目描述 "lalala,我是一个快乐的粉刷匠",小名一边快活地唱着歌,一边开 ...

  5. Go Example--通道同步

    package main import ( "fmt" "time" ) func main() { //缓存通道 done := make(chan bool ...

  6. linux杀死进程方法

    方法一: 根据pid ps -aux|grep 你的进程 方法二: 删除pid文件 [root@localhost ~]# /etc/init.d/redis_6380 start /var/run/ ...

  7. Linux内核Inotify机制学习笔记

    一.Inotify简介: Inotify是一种文件变化通知机制,Linux内核从2.6.13开始引入.它是一个内核用于通知用户空间程序文件系统变化的机制.开源社区提出用户态需要内核提供一些机制,以便用 ...

  8. Cassandra -- Cassandra 3.0版本安装

    ============================================================ 服务器信息 搭建三节点的Cassandra群集: SERVER1: 192.1 ...

  9. day 54 JS 之 jquery

    jQuery入门 2017-07-10 jQuery快速入门. jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Doc ...

  10. lch 儿童围棋课堂 启蒙篇 (李昌镐 著)

    第1章 了解围棋 第2章 无气不活 棋子的"气"第3章 有目数才能赢空第4章 常用术语第5章 吃子第6章 死活:眼第7章 死活:典型棋形第8章 布局:术语篇 第1章 了解围棋 (已 ...