Spring Boot 生命周期
- Spring Boot 事件
- Spring Boot 扩展
版本说明:
Spring Boot 2.1.4.RELEASE
Spring Boot 事件
ApplicationEvent 应用事件抽象
org.springframework.boot.context.event.SpringApplicationEvent Spring应用事件
ApplicationStartingEvent应用上下文启动事件可以调整
SpringApplication以及参数argsApplicationEnvironmentPreparedEventEnvironment准备事件可以调整
org.springframework.core.env.ConfigurableEnvironmentApplicationContextInitializedEvent应用上下文初始化事件可以调整
org.springframework.context.ConfigurableApplicationContextApplicationPreparedEvent应用准备事件最后可以调整
org.springframework.context.ConfigurableApplicationContextContextRefreshedEvent上下文刷新事件参数:
org.springframework.context.ApplicationContextServletWebServerInitializedEventWeb Servlet初始化事件参数:
Servlet应用上下文org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext以及web服务org.springframework.boot.web.server.WebServerApplicationStartedEvent应用启动完成事件参数:
SpringApplication、参数args以及org.springframework.context.ConfigurableApplicationContextApplicationReadyEvent应用预备事件参数:
org.springframework.context.ConfigurableApplicationContextContextClosedEvent上下文关闭事件参数:
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配置