400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

使用SpringBoot怎么监听应用事件

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

为临沂等地区用户提供了全套网页设计制作服务,及临沂网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、临沂网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

1. Spring Boot特有的应用事件

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

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

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

2. 如何监听这些事件

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

2.1. SpringApplication.addListeners(...)

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

2.2. SpringApplicationBuilder.listeners(...)

new SpringApplicationBuilder()
   .sources(StartEventsApplication.class)
   .listeners(
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) event -> log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName()),
     (ApplicationListener) 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 {
 @Override
 public void onApplicationEvent(ApplicationStartedEvent event) {
  log.info("----------- 监听Spring Boot:" + event.getClass().getSimpleName());
 }
}

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


文章题目:使用SpringBoot怎么监听应用事件
网址分享:http://mbwzsj.com/article/psphio.html

其他资讯

让你的专属顾问为你服务