SpringBoot核心机制三、ApplicationListener

所有调试均使用SpringBoot 2.4.5版本。

ApplicationListener事件监听机制其实是由Spring提供的,应用内部的事件驱动机制。也就是Pub/Sub发布订阅机制在应用内部的实现。一般主要是用于监控应用内部的一些运行状况,在应用开发中也可以使用。

具体的实现机制可以到Spring中去探究,这里就来简单理解下SpringBoot对这个事件驱动机制做了哪些封装。

一、事件监听器使用

1、自己实现一个事件监听器

首先创建一个自定义的事件监听器:

public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
   
   
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
   
   
        System.out.println("======>MyApplicationListener: "+applicationEvent);
    }
}

然后还是在项目的spring.factories中配置监听器

org.springframework.context.ApplicationListener=\
com.roy.applicationListener.MyApplicationListener

然后配置启动类。在启动类中发布一个自己的事件。

@SpringBootApplication
public class P1Application implements CommandLineRunner {
   
   
    public static void main(String[] args) {
   
   
        final SpringApplication application = new SpringApplication(P1Application.class);
//        application.addInitializers(new MyApplicationContextInitializer());
        application.run(args);
    }
    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
   
   
        //自行发布一个事件。
        applicationContext.publishEvent(new ApplicationEvent("selfEvent") {
   
   
        });
    }
}

正常启动SpringBoot应用,就能打印出启动过程中的关键事件的日志。这里把关键的事件日志给整理出来:

======>MyApplicationListener: org.springframework.boot.context.event.ApplicationStartingEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationContextInitializedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationPreparedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationStartedEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.availability.AvailabilityChangeEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
======>MyApplicationListener: com.roy.P1Application$1[source=selfEvent]  ##!!自行发布的事件。
======>MyApplicationListener: org.springframework.boot.context.event.ApplicationReadyEvent[source=org.springframework.boot.SpringApplication@60c6f5b]
======>MyApplicationListener: org.springframework.boot.availability.AvailabilityChangeEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]
======>MyApplicationListener: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@1d296da, started on Wed Apr 28 13:33:33 CST 2021]

这里就打印出了SpringBoot应用启动过程中的多个内部事件。实际上这多个内部事件也就对应了启动过程的各个阶段,是梳理SpringBoot启动流程非常好的入口。

Spring事件机制的其他细节这里就不多说了,大家可以自行了解。我们这里还是专注于SpringBoot的部分。

2、事件监听器的其他配置方式:

这个事件监听机制是Spring非常重要的一个机制,有非常多的配置方式。除了上面提到的基于spring.factories文件配置的方式,还有其他几种配置方式。

2.1 SpringApplication.addListener

跟之前的Initializer一样,这个事件监听器也可以在SpringApplication中直接添加。

@SpringBootApplication
public class P1Application implements CommandLineRunner {
    public static void main(String[] args) {
        final SpringApplication application = new SpringApplication(P1Application.class);
        //添加事件监听器
        application.addListeners(new MyApplicationListener());
        application.run(args);
    }
}

2.2 基于注解添加

基于注解,将MyApplicationListener配置到Spring的IOC容器中。

@Configuration
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
   
   
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
   
   
        System.out.println("======>MyApplicationListener: "+applicationEvent);
    }
}

2.3 在SpringBoot的配置文件中配置

另外还一种方式,可以在SpringBoot的配置文件application.properties中配置

context.listener.classes=com.roy.applicationListener.MyApplicationListener

这几种方式都可以配置事件监听器。另外,其实在上一章节ApplicationContextInitializer中也能看到,在SpringBoot内部也在应用初始化中扩展出了很多通过application添加事件监听器的扩展。

二、核心机制解读

事件机制的使用方式很多,不同的配置方式也有不同的加载流程。我们这里还是只解读SpringBoot中如何通过spring.factories文件来加载事件监听器的。

SpringBoot中对于监听器的处理,也跟ApplicationContextInitializer的处理流程是差不多的。首先在SpringApplication的构造方法中加载所有的监听器:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
   
   
       ...
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    	//加载spring.facotries中注册的所有事件监听器
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        ...
    }

然后在SpringApplication的run方法中启动所有监听器:

public ConfigurableApplicationContext run(String..
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

roykingw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值