Springboot生命周期
application.properties中增加
context.listener.classes=demo.springboot.listener.ApplicationEventListener
实现接口ApplicationListener
package demo.springboot.listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextStartedEvent; import org.springframework.context.event.ContextStoppedEvent; /** * springboot 生命周期 * @author ardo */ public class ApplicationEventListener implements ApplicationListener<ApplicationEvent> { private Logger logger = LoggerFactory.getLogger(getClass()); @Override public void onApplicationEvent(ApplicationEvent event) { // 在这里可以监听到Spring Boot的生命周期 if(event instanceof ApplicationStartingEvent){ logger.info("应用程序启动中"); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { // 初始化环境变量 logger.info("初始化环境变量"); } else if (event instanceof ApplicationPreparedEvent) { // 初始化环境变量完成,初始化上下文context完成 logger.info("初始化环境变量完成,初始化上下文context完成"); } else if (event instanceof ContextRefreshedEvent) { // 应用刷新 logger.info("应用刷新"); } else if (event instanceof ApplicationReadyEvent) {// 应用已启动完成 logger.info("应用已启动完成"); } else if (event instanceof ContextStartedEvent) { // 应用启动,Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的 Start()方法开始/重新开始容器时触发该事件。 logger.info("应用启动好了"); } else if(event instanceof ApplicationFailedEvent){ logger.info("应用启动失败"); }else if (event instanceof ContextStoppedEvent) { // 应用停止,Spring2.5新增的事件,当容器调用ConfigurableApplicationContext 的Stop()方法停止容器时触发该事件。 logger.info("应用停止"); } else if (event instanceof ContextClosedEvent) { // 应用关闭,当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有 单例Bean都被销毁。 logger.info("应用关闭"); } else { logger.info("其他事件" + event.toString()); } } }
ApplicationContextInitializedEvent:当 SpringApplication 启动并且准备好 ApplicationContext,并且在加载任何 bean 定义之前调用了 ApplicationContextInitializers 时发布的事件。对应的生命周期方法是contextPrepared()。
ApplicationPreparedEvent:它是SpringBoot上下文 context 创建完成是发布的事件;但此时 spring 中的 bean 还没有完全初始化完成,因为后面还要refreshContext。这里可以将上下文传递出去做一些额外的操作。但是在该监听器中是无法获取自定义 bean 并进行操作的。对应的生命周期方法是 contextLoaded()。
ApplicationStartedEvent:这个事件是在 2.0 版本才引入的;具体发布是在应用程序上下文刷新之后,调用任何 ApplicationRunner 和 CommandLineRunner 运行程序之前。
SpringApplicationRunListeners:是一个集合类,内部包含一个 log 和包含 SpringApplicationRunListener 的 List。而 SpringApplicationRunListener 主要是监听 SpringApplication 对象的,里面的方法都定义了在何时调用 SpringApplicationRunListener 的各种方法,它的实现类是EventPublishingRunListener。它就是一个ApplicationListener的代理。springboot启动的几个主要过程的监听通知都是通过他来进行回调,它的生命周期就是从开始启动,到启动结束。
下面的每一个方法 SpringApplicationRunListener 都把其包装成一个事件,在spring容器还未成功 refreshed 之前都是使用SimpleApplicationEventMulticaster 去寻找对该事件感兴趣的ApplicationListener,然后调用其onApplicationEvent方法
starting:当SpringApplication对象的run方法刚启动的时候(依靠SimpleApplicationEventMulticaster),对应的事件:ApplicationStartingEvent
environmentPrepared:在environment Prepared 但是spring容器还未创建的时候(依靠SimpleApplicationEventMulticaster),对应的事件:ApplicationPreparedEvent
contextPrepared:当spring容器已经创建且准备好了,(目前是空的实现),这时候environment资源还没初始化完成。
contextLoaded:当spring容器已经loaded 且未refreshContext 。这个时候environment资源也初始化完成。load就是将我们的primaryClass注册到spring容器中,(依靠SimpleApplicationEventMulticaster) 同时将之前获取到的ApplicationListener都加入到spring容器中,此时如果ApplicationListener还是ApplicationContextAware的也要调用其setApplicationContext方法。对应的事件:ApplicationPreparedEvent
started:spring容器已经刷新过且应用已经启动,但是CommandLineRunners和ApplicationRunners还未调用,直接通过spring容器自己发送(因为ApplicationListener已经加入spring容器),对应的事件:ContextStartedEvent
running:我们已经调用了CommandLineRunners,直接通过spring容器自己发送(因为ApplicationListener已经加入spring容器),对应的事件:ApplicationReadEvent
failed:当异常发生的时候就调用这个,如果spring容器没有loaded 或者没有激活就使用SimpleApplicationEventMulticaster,否则还是依靠spring容器自己。对应该事件:ApplicationFailedEvent