Spring Bean生命周期initMessageSource&initApplicationEventMulticaster(八)

现在到了ApplicationContext的refresh()方法中初始化消息源与事件广播器,这两个方法比较像,并且逻辑较少,放在一起看看。

initMessageSource

判断beanFactory中是否有名字为messageSource的bean,如果有,从beanFactory中获取并且判断获取的是不是HierarchicalMessageSource类型的,如果是设置其父级消息源

如果没有,新建DelegatingMessageSource类作为messageSource的Bean。

protected void initMessageSource() {
      //获取BeanFactory
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        
        //判断beanFactory中是否有名字为messageSource的bean
        // MESSAGE_SOURCE_BEAN_NAME = "messageSource";
        // 如果没有,新建DelegatingMessageSource类作为messageSource的Bean。
        // 如果有,从beanFactory中获取
        
        if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
            this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
            // Make MessageSource aware of parent MessageSource.
            if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
                HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
                if (hms.getParentMessageSource() == null) {
                    // Only set parent context as parent MessageSource if no parent MessageSource
                    // registered already.
                    hms.setParentMessageSource(getInternalParentMessageSource());
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Using MessageSource [" + this.messageSource + "]");
            }
        }
        else {
            // Use empty MessageSource to be able to accept getMessage calls.
            DelegatingMessageSource dms = new DelegatingMessageSource();
            dms.setParentMessageSource(getInternalParentMessageSource());
            this.messageSource = dms;
            beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
                        "': using default [" + this.messageSource + "]");
            }
        }
    }

initApplicationEventMulticaster

初始化ApplicationEventMulticaster,如果beanFactory中不存在的话使用SimpleApplicationEventMulticaster,如果存在使用从beanFactory中获取的。

protected void initApplicationEventMulticaster() {
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        //APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";
        if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
            this.applicationEventMulticaster =
                    beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
            if (logger.isDebugEnabled()) {
                logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
            }
        }
        else {
            this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
            beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
                        APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
                        "': using default [" + this.applicationEventMulticaster + "]");
            }
        }
    }

onRefresh

再多说一个onrefresh方法,这个在Spring框架中是没有实现的,留待我们继承可用来在实例化Singleton之前,实例化一些特殊的bean。

registerListeners

这个方法再refresh中也比较简单,都顺便记录下。

  1. 获取ApplicationContext初始化的listeners,然后添加到EventMulticaster,这个刚才初始化的,如果为空为SimpleApplicationEventMulticaster的那个
  2. 从BeanFactory中获取ApplicationListener类型的Bean,并且添加为ListenerBeans
  3. 有些事件可能要提前发出,将需要提前发出的事件发出并置空。
protected void registerListeners() {
        // Register statically specified listeners first.
        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!
        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);
            }
        }
    }

TODO

  • AbstractApplicationEventMulticaster类的详细设计

最后

这几个方法比较简单,一起看看

上一篇:乐观并发控制(Optimistic Concurrency)


下一篇:新鲜出炉的路透图,Waymoo自动驾驶卡车路测疑被曝光