SpringBoot源码分析(七) -- 注册监听器

573 阅读1分钟

SpringBoot源码分析(七) -- 注册监听器

registerListeners 方法

protected void registerListeners() {
    // Register statically specified listeners first.
    //先获取已经添加的ApplicationListener监听器
    for (ApplicationListener<?> listener : getApplicationListeners()) {
        getApplicationEventMulticaster().addApplicationListener(listener);
    }

    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let post-processors apply to them!
    //从beanFactory中获取所有ApplicationListener监听器对应的beanName
    //这里不会初始化这些对应的bean,会等到后面的后置处理器来初始化
    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
    for (String listenerBeanName : listenerBeanNames) {
        getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
    }

    // Publish early application events now that we finally have a multicaster...
    //发布earlyApplicationEvents中的事件
    Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
    this.earlyApplicationEvents = null;
    if (earlyEventsToProcess != null) {
        for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
            getApplicationEventMulticaster().multicastEvent(earlyEvent);
        }
    }
}