Spring - IoC 源码分析:AbstractApplicationContext - registerListeners()

in TCEHJava with 0 comment
    protected void registerListeners() {
        // 优先注册静态监听器
        // Register statically specified listeners first.
        for (ApplicationListener<?> listener : getApplicationListeners()) {
            // 将应用监听器添加到应用事件多播器
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        // 需要保留所有常规Bean,留给后处理器来处理
        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let post-processors apply to them!
        // 注册常规应用监听器
        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...
        // 发布应用事件
        Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
        this.earlyApplicationEvents = null;
        if (earlyEventsToProcess != null) {
            for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
                // 广播事件
                getApplicationEventMulticaster().multicastEvent(earlyEvent);
            }
        }
    }