温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Spring Boot怎么监听应用事件

发布时间:2021-04-09 16:00:14 来源:亿速云 阅读:248 作者:Leah 栏目:编程语言

使用Spring Boot怎么监听应用事件?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1. Spring Boot特有的应用事件

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

  • ApplicationStartingEvent:在任何处理(除了注册listener和initializer)开始之前发送。

  • ApplicationEnvironmentPreparedEvent: 在context创建之前,而用到context中的Environment已经被识别时发送。

  • ApplicationContextInitializedEvent: SpringApplication正在启动,ApplicationContext已准备好且ApplicationContextInitializer已被调用但是bean的定义还没有被加载时发送。

  • ApplicationPreparedEvent: 在context刷新之前,在bean的定义已经被加载之后调用。

  • ApplicationStartedEvent: 在任何应用和command-line runner调用之前,而context已经被刷新时发送。

  • ApplicationReadyEvent: 在任何应用和command-line runner被调用的时候发送,它意味着应用可以接受请求了。

  • ApplicationFailedEvent: 在启动时有异常的时候发送。

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

  • 注解了@EventListener注解分方法的类注册的bean;

  • 实现了ApplicationListener<Event>接口的类注册的bean。

像ApplicationStartedEvent和ApplicationReadyEvent是ApplicationContext创建之后触发的,可以用上述两种方式来监听事件。

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI