Spring Boot应用事件监听

栏目: Java · 发布时间: 6年前

内容简介:除了Spring框架的事件,Spring Boot的有些事件是在像

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的事件监听方式:

@EventListener
ApplicationListener<Event>

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(...)

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

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

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());
    }
}

以上所述就是小编给大家介绍的《Spring Boot应用事件监听》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

软件测试经验与教训

软件测试经验与教训

Cem Kaner、James Bach、Bret Pettichord / 机械工业出版社 / 2004-1 / 35.00

本书汇总了293条来自软件测试界顶尖专家的经验与建议,阐述了如何做好测试工作、如何管理测试,以及如何澄清有关软件测试的常见误解,读者可直接将这些建议用于自己的测试项目工作中。这些经验中的每一条都是与软件测试有关的一个观点,观点后面是针对运用该测试经验的方法、时机和原因的解释或例子。 本书还提供了有关如何将本书提供的经验有选择性地运用到读者实际项目环境中的建议,在所有关键问题上所积累的经验,以......一起来看看 《软件测试经验与教训》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具