一、spring boot事件类型
事件 | 触发时机 | 常用监听器描述 |
---|---|---|
ApplicationStartingEvent | 应用启动时 | LoggingApplicationListener:决定加载哪个日志系统 |
ApplicationEnvironmentPreparedEvent | 创建Environment之后 | BootstrapApplicationListener:用于加载外部配置和初始化与 Spring Cloud 相关的组件; LoggingSystemShutdownListener:清理日志系统资源; EnvironmentPostProcessorApplicationListener:调用EnvironmentPostProcessor实现类,如加载application-*.yml配置文件,bootstrap配置文件; LoggingApplicationListener:初始化日志系统; BackgroundPreinitializer:异步执行一些与初始化任务; DelegatingApplicationListener:读取配置文件中的context.listener.classes 自定义监听器类,实例化并在加入(自定义监听器的顺序是独立的) |
ApplicationContextInitializedEvent | 创建applicationContext并调用ApplicationContextInitializer之后 | |
ApplicationPreparedEvent | 加载xml bean文件之后 | LoggingApplicationListener:将日志系统注入容器 |
ContextRefreshedEvent | refreshContext流程结束之后 | RestartListener:重启后重新出发ApplicationEnvironmentPreparedEvent事件 |
ApplicationStartedEvent | 自定义afterRefresh方法调用之后 | |
ApplicationReadyEvent | spring-boot启动结束时 | |
ApplicationFailedEvent | spring-boot启动失败时 | |
ContextClosedEvent | ParentContextCloserApplicationListener:在context关闭时,同时关闭父contenxt |