Spring生命周期事件

149 阅读2分钟

SpringBoot生命周期事件

在使用@EventListener执行功能的时候如果需要异步执行,只需要加上@Async注解接口

ApplicationStartingEvent-在运行开始时但在任何处理之前

需要实现BootstrapRegistryInitializer,通过classpath:META-INF/spring.factories中配置

  1. 实现BootstrapRegistryInitializer
    public static class ApplicationStartingListener implements BootstrapRegistryInitializer {
        @Override
        public void initialize(BootstrapRegistry registry) {
            log.info("在运行开始时但在任何处理之前发送ApplicationStartingEvent");
        }
    }
  1. 配置spring.factories

org.springframework.boot.BootstrapRegistryInitializer=cn.mj.web.springboot01.event.ApplicationListener.ApplicationStartingListener

ApplicationEnvironmentPreparedEvent-在已知要在上下文中使用的Environment但在上下文创建之前

需要实现SpringApplicationRunListener,通过classpath:META-INF/spring.factories中配置

  1. 实现SpringApplicationRunListener
    public static class ApplicationEnvironmentPreparedListener implements SpringApplicationRunListener{
        @Override
        public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
            log.info("在已知要在上下文中使用的Environment但在上下文创建之前,发送ApplicationEnvironmentPreparedEvent");
        }
    }
  1. 配置spring.factories

org.springframework.boot.SpringApplicationRunListener=cn.mj.web.springboot01.event.ApplicationListener.ApplicationEnvironmentPreparedListener

ApplicationContextInitializedEvent-在准备好ApplicationContext并调用ApplicationContextInitializers之后但在加载任何bean定义之

需要实现ApplicationListener,通过SpringApplication进行配置

  1. 实现ApplicationListener
    public static class ApplicationContextInitializedListener implements org.springframework.context.ApplicationListener<ApplicationContextInitializedEvent>{
        @Override
        public void onApplicationEvent(ApplicationContextInitializedEvent event) {
            log.info("在准备好ApplicationContext并调用ApplicationContextInitializers之后但在加载任何bean定义之前:{}",event.getClass().getSimpleName());
        }
    }
  1. 配置SpringApplication
@SpringBootApplication
public class SpringBoot01Application {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpringBoot01Application.class);
        springApplication.addListeners(
                new ApplicationListener.ApplicationContextInitializedListener()
        );
        springApplication.run(args);
    }
}

ApplicationPreparedEvent-在刷新开始之前但在加载bean定义之后

需要实现ApplicationListener,通过SpringApplication进行配置

  1. 实现ApplicationListener
    public static class ApplicationPreparedListener implements org.springframework.context.ApplicationListener<ApplicationPreparedEvent>{
        @Override
        public void onApplicationEvent(ApplicationPreparedEvent event) {
            log.info("在刷新开始之前但在加载bean定义之后:{}",event.getClass().getSimpleName());
        }
    }
  1. 配置SpringApplication
@SpringBootApplication
public class SpringBoot01Application {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpringBoot01Application.class);
        springApplication.addListeners(
                new ApplicationListener.ApplicationPreparedListener()
        );
        springApplication.run(args);
    }
}

WebServerInitializedEvent-webserver准备好了

直接通过@EventListener注解监听

    @EventListener
    public void onApplicationEvent(WebServerInitializedEvent event) {
        log.info("webserver准备好了:{}",event.getClass().getSimpleName());
    }

ContextRefreshedEvent-上下文刷新完成

直接通过@EventListener注解监听

    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("ApplicationContext刷新:{}",event.getClass().getSimpleName());
    }

ApplicationStartedEvent-在上下文刷新后但在调用任何应用程序和命令行运行器之前

直接通过@EventListener注解监听

    @EventListener
    public void onApplicationEvent(ApplicationStartedEvent event) {
        log.info("在上下文刷新后但在调用任何应用程序和命令行运行器之前:{}",event.getClass().getSimpleName());
    }

AvailabilityChangeEvent-程序可以被认为是正常活动了

直接通过@EventListener注解监听。有几种状态。正常的状态为:CORRECT、ACCEPTING_TRAFFIC。 当关闭程序时,状态为:REFUSING_TRAFFIC。 可以用于给k8s之类的容器健康检查。

    @EventListener
    public void onApplicationEvent(AvailabilityChangeEvent event) {
        log.info("程序可以被认为是正常活动了:{},此时状态:{}",event.getClass().getSimpleName(),event.getState());
    }

CommandLineRunner、ApplicationRunner

直接注册实现类到Spring容器里即可

@Slf4j
@Configuration
public class CustomerCommandRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        log.info("执行commandRunner,参数为:{}",args);
    }
}

ApplicationReadyEvent-最终启动完成

直接通过@EventListener注解监听

    @EventListener
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("在调用任何应用程序和命令行运行器之后:{}",event.getClass().getSimpleName());
    }

ApplicationFailedEvent-启动失败

直接通过@EventListener注解监听

    @EventListener
    public void onApplicationEvent(ApplicationFailedEvent event) {
        log.info("启动失败:{}",event.getClass().getSimpleName());
    }