Spring Boot应用事件监听

作者: wiselyman 分类: Spring 发布时间: 2018-12-04 11:47

1. Spring Boot特有的应用事件

除了Spring框架的事件,Spring Boot的SpringApplication也发送了一些自己的事件:

  1. ApplicationStartingEvent:在任何处理(除了注册listenerinitializer)开始之前发送。
  2. ApplicationEnvironmentPreparedEvent: 在context创建之前,而用到context中的Environment已经被识别时发送。
  3. ApplicationContextInitializedEventSpringApplication正在启动,ApplicationContext已准备好且ApplicationContextInitializer已被调用但是bean的定义还没有被加载时发送。
  4. ApplicationPreparedEvent: 在context刷新之前,在bean的定义已经被加载之后调用。
  5. ApplicationStartedEvent: 在任何应用和command-line runner调用之前,而context已经被刷新时发送。
  6. ApplicationReadyEvent: 在任何应用和command-line runner被调用的时候发送,它意味着应用可以接受请求了。
  7. ApplicationFailedEvent: 在启动时有异常的时候发送。

有些事件是在ApplicationContext创建之前触发的,所以我们不能用常规的注册成bean的事件监听方式:

  1. 注解了@EventListener注解分方法的类注册的bean;
  2. 实现了ApplicationListener<Event>接口的类注册的bean。

ApplicationStartedEventApplicationReadyEventApplicationContext创建之后触发的,可以用上述两种方式来监听事件。

2. 如何监听这些事件

我们可以通过下面的方式注册监听:

2.1. SpringApplication.addListeners(...)

SpringApplication application = new SpringApplication(StartEventsApplication.class);
application.addListeners(
        (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
        (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
        (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
        (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
        (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
        (ApplicationListener<ApplicationReadyEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName())
);
application.run(args);

2.2. SpringApplicationBuilder.listeners(...)

new SpringApplicationBuilder()
            .sources(StartEventsApplication.class)
            .listeners(
                    (ApplicationListener<ApplicationStartingEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
                    (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
                    (ApplicationListener<ApplicationContextInitializedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
                    (ApplicationListener<ApplicationPreparedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
                    (ApplicationListener<ApplicationStartedEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
                    (ApplicationListener<ApplicationReadyEvent>) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName())
                    )
            .run(args);

2.3. META-INF/spring.factories

src/main/resources/META-INF/spring.factories

org.springframework.context.ApplicationListener=top.wisely.startevents.listeners.ApplicationContextInitializedEventListener, \
                                                top.wisely.startevents.listeners.ApplicationEnvironmentPreparedEventListener, \
                                                top.wisely.startevents.listeners.ApplicationPreparedEventListener, \
                                                top.wisely.startevents.listeners.ApplicationReadyEventListener, \
                                                top.wisely.startevents.listeners.ApplicationStartedEventListener, \
                                                top.wisely.startevents.listeners.ApplicationStartingEventListener

监听器只需实现ApplicationListener<要监听的接口类型>接口,无需手动注册为bean:

public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName());
    }
}

3. 源码地址

https://github.com/wiselyman/spring-boot-application-events.git

发表评论

电子邮件地址不会被公开。 必填项已用*标注