SpringBoot中GenericApplicationListener如何实现事件监听?

2026-06-10 16:571阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计431个文字,预计阅读时间需要2分钟。

SpringBoot中GenericApplicationListener如何实现事件监听?

ApplicationContext是Spring框架的核心,通常我们将其理解为上下文环境。实际上,容器会更好一些。ApplicationContext是应用的容器,Spring将Bean(对象)放置在其中。需要使用g来引用它。

什么是ApplicationContext?

它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。 ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。

ApplicationEvent

  • 是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
  • springboot的event的类型:
    • ApplicationStartingEvent
    • ApplicationEnvironmentPreparedEvent
    • ApplicationContextInitializedEvent
    • ApplicationPreparedEvent
    • ContextRefreshedEvent
    • ServletWebServerInitializedEvent
    • ApplicationStartedEvent
    • ApplicationReadyEvent

ApplicationListener

是一个接口,里面只有一个onApplicationEvent方法。所以自己的类在实现该接口的时候,要实现该方法。

SpringBoot中GenericApplicationListener如何实现事件监听?

ApplicationListener的封装类

  • GenericApplicationListener
  • GenericApplicationListenerAdapter
  • SmartApplicationListener

关系

如果在上下文中部署一个实现了ApplicationListener接口的bean,那么每当在一个ApplicationEvent发布到 ApplicationContext时,这个bean得到通知。其实这就是标准的Oberver设计模式。

注意

要配置META-INF/spring.factories文件,并在文件中实现

使用

// 第一种方式 public class AiInfluxdbApplicationListener implements GenericApplicationListener { @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } @Override public boolean supportsEventType(ResolvableType eventType) { return ApplicationReadyEvent.class.isAssignableFrom(eventType.getRawClass()); } @Override public void onApplicationEvent(ApplicationEvent event) { System.out.print("here is ApplicationReadyEvent"); } } //第二种方式 public class ConfigApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered { @Override public int getOrder() { return HIGHEST_PRECEDENCE; } @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { } } //META-INF/spring.factories文件定义 org.springframework.context.ApplicationListener=\ com.demotest.core.ApplicationStartListener

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计431个文字,预计阅读时间需要2分钟。

SpringBoot中GenericApplicationListener如何实现事件监听?

ApplicationContext是Spring框架的核心,通常我们将其理解为上下文环境。实际上,容器会更好一些。ApplicationContext是应用的容器,Spring将Bean(对象)放置在其中。需要使用g来引用它。

什么是ApplicationContext?

它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。 ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。

ApplicationEvent

  • 是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
  • springboot的event的类型:
    • ApplicationStartingEvent
    • ApplicationEnvironmentPreparedEvent
    • ApplicationContextInitializedEvent
    • ApplicationPreparedEvent
    • ContextRefreshedEvent
    • ServletWebServerInitializedEvent
    • ApplicationStartedEvent
    • ApplicationReadyEvent

ApplicationListener

是一个接口,里面只有一个onApplicationEvent方法。所以自己的类在实现该接口的时候,要实现该方法。

SpringBoot中GenericApplicationListener如何实现事件监听?

ApplicationListener的封装类

  • GenericApplicationListener
  • GenericApplicationListenerAdapter
  • SmartApplicationListener

关系

如果在上下文中部署一个实现了ApplicationListener接口的bean,那么每当在一个ApplicationEvent发布到 ApplicationContext时,这个bean得到通知。其实这就是标准的Oberver设计模式。

注意

要配置META-INF/spring.factories文件,并在文件中实现

使用

// 第一种方式 public class AiInfluxdbApplicationListener implements GenericApplicationListener { @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; } @Override public boolean supportsEventType(ResolvableType eventType) { return ApplicationReadyEvent.class.isAssignableFrom(eventType.getRawClass()); } @Override public void onApplicationEvent(ApplicationEvent event) { System.out.print("here is ApplicationReadyEvent"); } } //第二种方式 public class ConfigApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered { @Override public int getOrder() { return HIGHEST_PRECEDENCE; } @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { } } //META-INF/spring.factories文件定义 org.springframework.context.ApplicationListener=\ com.demotest.core.ApplicationStartListener

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。