【六】springboot启动源码 - registerListeners

registerListeners 源码解析

Check for listener beans and register them.
监听器注册

自定义监听事件

package org.hopehomi.core.tool.support.listener;

import org.springframework.context.ApplicationEvent;

/**
 * @author Ledison
 * @date 2023/3/22
 */
public class ForDebuggerEvent extends ApplicationEvent {

    public ForDebuggerEvent(Object source) {
        super(source);
    }

    public void executeEvent() {
        System.out.println("ForDebuggerEvent executeEvent");
    }

}

自定义事件监听器

package org.hopehomi.core.tool.support.listener;

import org.springframework.context.ApplicationListener;

/**
 *
 * @author Ledison
 * @date 2023/3/22
 */
public class ForDebuggerEventListener implements ApplicationListener<ForDebuggerEvent> {

    @Override
    public void onApplicationEvent(ForDebuggerEvent event) {
        event.executeEvent();
    }
}

自定义事件生产类

package org.hopehomi.core.tool.support.listener;

import cn.hutool.extra.spring.SpringUtil;

import java.util.HashMap;

/**
 * @author Ledison
 * @date 2023/3/22
 */
public class ForDebuggerEventPusher {

    public static void pushEvent() {
        SpringUtil.publishEvent(new ForDebuggerEvent(new HashMap<>()));
    }
}

ApplicationListener实现类什么时候被加载并实例化的

在SpringApplication的构造方法中
在这里插入图片描述
getSpringFactoriesInstances方法
依然是通过SpringFactoriesLoader.loadFactoryNames(Class<?> factoryType, ClassLoader classLoader),查找spring.factories下key为interface org.springframework.context.ApplicationListener的类名。
获取到类名后,通过构造器对这些类进行实例化
在这里插入图片描述
返回实例集合后,再将这些对象放入SpringApplication中
在这里插入图片描述

在这里插入图片描述
当SpringApplication开始调用run方法,在创建context之后的prepareContext方法中,会将SpringApplication的listeners设置到contextapplicationListeners中
在这里插入图片描述
prepareContext方法
在这里插入图片描述

contextLoaded方法
在这里插入图片描述

补充说明,可以看到这里有很多类似的方法,他们都是根据SpringApplicationRunListener的回调时机来触发
在这里插入图片描述

doWithListeners方法
在这里插入图片描述
补充说明一下,这里的listeners是基于spring SPI查询到的key为org.springframework.boot.SpringApplicationRunListener的实现类

在这里插入图片描述
在这里插入图片描述
遍历SpringApplicationRunListener,并通过Consumer执行listener.contextLoaded(context)在这里插入图片描述
调用EventPublishingRunListener重写方法contextLoaded,并将SpringApplication的listeners设置到context的applicationListeners中

在这里插入图片描述

进入正题registerListeners

  1. 将context的applicationListeners,注册到事件控制器(context的ApplicationEventMulticaster applicationEventMulticaster,默认实现类是SimpleApplicationEventMulticaster)的内部类(DefaultListenerRetriever)的Set<ApplicationListener<?>> applicationListeners

Register statically specified listeners first.

通过spring spi 加载org.springframework.context.ApplicationListener
在这里插入图片描述
在这里插入图片描述

  1. 通过beanFactory的beanDefinitionNames获取类型为ApplicationListener的bean名称集合,注册到事件控制器的applicationListenerBeans

Do not initialize FactoryBeans here: We need to leave all regular beans
uninitialized to let post-processors apply to them!

通过spring spi 加载org.springframework.boot.autoconfigure.EnableAutoConfiguration
在这里插入图片描述
在这里插入图片描述
3. 加载earlyApplicationEvents到事件控制器,暂时不分析

Publish early application events now that we finally have a multicaster…

在这里插入图片描述

总结

  1. 通过spring spi 加载ApplicationListener的类,实例化后注入到context的applicationEventMulticaster中,DefaultListenerRetriever的applicationListeners集合
  2. 通过spring spi 加载EnableAutoConfiguration的类,将beanName注入到context的applicationEventMulticaster中,DefaultListenerRetriever的applicationListenerBeans集合
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值