Spring Boot 生命周期
- Spring Boot 事件
- 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
配置