SpringBoot--启动时的事件机制

原文网址:SpringBoot--启动时的事件机制_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文介绍SpringBoot启动过程中的事件机制。

        SpringBoot的启动过程用到了事件监听机制,它属于观察者模式。学习事件监听机制可以让我们更好地理解SpringBoot的启动流程,也有利于将观察者模式应用于项目。

系列文章

  1. SpringBoot--自定义事件--同步与异步_IT利刃出鞘的博客-CSDN博客_springboot监听器的原理和用途
  2. SpringBoot--自定义事件--方法/详解/实例_IT利刃出鞘的博客-CSDN博客
  3. SpringBoot--启动时的事件机制_IT利刃出鞘的博客-CSDN博客

SpringBoot启动过程的事件

启动事件(按先后顺序)

  1. ApplicationStartingEvent
    1. 这个事件在 Spring Boot 应用运行开始时,且进行任何处理之前发送(除了监听器和初始化器注册之外)。
  2. ApplicationEnvironmentPreparedEvent
    1. 这个事件在当已知要在上下文中使用 Spring 环境(Environment)时,在 Spring 上下文(context)创建之前发送。
  3. ApplicationContextInitializedEvent
    1. 这个事件在当 Spring 应用上下文(ApplicationContext)准备好了,并且应用初始化器(ApplicationContextInitializers)已经被调用,在 bean 的定义(bean definitions)被加载之前发送。
  4. ApplicationPreparedEvent
    1. 这个事件是在 Spring 上下文(context)刷新之前,且在 bean 的定义(bean definitions)被加载之后发送。
  5. ApplicationStartedEvent
    1. 这个事件是在 Spring 上下文(context)刷新之后,且在 application/ command-line runners 被调用之前发送。
  6. AvailabilityChangeEvent
    1. 这个事件紧随上个事件之后发送,状态:ReadinessState.CORRECT,表示应用已处于活动状态。
  7. ApplicationReadyEvent
    1. 这个事件在任何 application/ command-line runners 调用之后发送。
  8. AvailabilityChangeEvent
    1. 这个事件紧随上个事件之后发送,状态:ReadinessState.ACCEPTING_TRAFFIC,表示应用可以开始准备接收请求了。
  9. ApplicationFailedEvent
    1. 这个事件在应用启动异常时进行发送。

        上面所介绍的这些事件列表仅包括绑定到 SpringApplication 的 SpringApplicationEvents 事件,除了这些事件以外,以下事件也会在 ApplicationPreparedEvent 之后和 ApplicationStartedEvent 之前发送:

  1. WebServerInitializedEvent
    1. 这个 Web 服务器初始化事件在 WebServer 启动之后发送,对应的还有 ServletWebServerInitializedEvent(Servlet Web 服务器初始化事件)、ReactiveWebServerInitializedEvent(响应式 Web 服务器初始化事件)。
  2. ContextRefreshedEvent
    1. 这个上下文刷新事件是在 Spring 应用上下文(ApplicationContext)刷新之后发送。 

原理

源码分析

myPublisher.myPublish("Hello") //HelloController
    applicationContext.publishEvent(new MyEvent(message)); //MyPublisher
        publishEvent(event, (ResolvableType)null); //AbstractApplicationContext.class
            // AbstractApplicationContext
            getApplicationEventMulticaster().multicastEvent((ApplicationEvent)applicationEvent, eventType);
                // AbstractApplicationContext
                applicationEventMulticaster.multicastEvent((ApplicationEvent)applicationEvent, eventType) 
                    
SimpleApplicationEventMulticaster#multicastEvent
以下都在SimpleApplicationEventMulticaster
multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType)
    // 此时,type为:com.example.tmp.MyEvent
    ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
        for (ApplicationListener<?> listener : getApplicationListeners(event, type))
            invokeListener(listener, event)
                
                
AbstractApplicationEventMulticaster#getApplicationListeners
getApplicationListeners(event, type))
    retriever = new ListenerRetriever(true);
    Collection<ApplicationListener<?>> listeners = retrieveApplicationListeners(eventType, sourceType, retriever);
        listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
            遍历listeners,如果监听器支持此事件,则加入集合并返回。

其他网址

Spring Boot 启动事件和监听器,太强大了!_Java技术栈,分享最主流的Java技术-CSDN博客

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/125396901