Spring Boot 生命周期

  1. Spring Boot 事件
  2. Spring Boot 扩展

版本说明:Spring Boot 2.1.4.RELEASE

Spring Boot 事件

ApplicationEvent 应用事件抽象

org.springframework.boot.context.event.SpringApplicationEvent Spring应用事件

  • ApplicationStartingEvent 应用上下文启动事件

    可以调整SpringApplication以及参数args

  • ApplicationEnvironmentPreparedEvent Environment准备事件

    可以调整org.springframework.core.env.ConfigurableEnvironment

  • ApplicationContextInitializedEvent 应用上下文初始化事件

    可以调整org.springframework.context.ConfigurableApplicationContext

  • ApplicationPreparedEvent 应用准备事件

    最后可以调整org.springframework.context.ConfigurableApplicationContext

  • ContextRefreshedEvent 上下文刷新事件

    参数:org.springframework.context.ApplicationContext

  • ServletWebServerInitializedEvent Web Servlet初始化事件

    参数:Servlet应用上下文org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext 以及web服务org.springframework.boot.web.server.WebServer

  • ApplicationStartedEvent 应用启动完成事件

    参数:SpringApplication、参数args以及org.springframework.context.ConfigurableApplicationContext

  • ApplicationReadyEvent 应用预备事件

    参数:org.springframework.context.ConfigurableApplicationContext

  • ContextClosedEvent 上下文关闭事件

    参数:org.springframework.context.ApplicationContext

SpringApplicationRunListener Spring 应用事件监听

默认实现:org.springframework.boot.context.event.EventPublishingRunListener

  • org.springframework.context.event.ApplicationListener 事件监听

  • org.springframework.context.event.SmartApplicationListener 好用的事件监听

    实现方式:实现ApplicationListener或者SmartApplicationListener,在META-INF\spring.factories文件中org.springframework.context.ApplicationListener配置

Lifecycle 生命周期,一般用于组件开启

  • org.springframework.context.SmartLifecycle 好用的生命周期

只要申明为Bean就可以调用,在ContextRefreshedEvent事件之前调用,

org.springframework.context.support.AbstractApplicationContext#finishRefresh方法调用

org.springframework.context.LifecycleProcessor#onRefresh进行调用

Spring Boot 扩展

ApplicationContextInitializer应用上下文初始化器

ApplicationEnvironmentPreparedEvent之后,ApplicationContextInitializedEvent之前

实现方式:实现ApplicationContextInitializer,在META-INF\spring.factories文件中org.springframework.context.ApplicationContextInitializer配置


Spring Boot 生命周期
http://example.com/2020/05/13/SpringBoot/SpringBoot 生命周期/
作者
FelixFly
发布于
2020年5月13日
许可协议