13-Spring refresh parsing the source (6) - initMessageSource [] and [] initApplicationEventMulticaster

Previous: Refresh the Spring-12 is the parse source (5) - registerBeanPostProcessors

Previous analysis we finished registerBeanPostProcessors, then we begin to analyze refreshthe methods of the seventh initMessageSourceand eighth methodsinitApplicationEventMulticaster

One,initMessageSource

Initialization MessageSourcecomponent (make binding message, message parsing)

	protected void initMessageSource() {
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		// 判断是否已经存在名为messageSource的Bean了
		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.isTraceEnabled()) {
				logger.trace("Using MessageSource [" + this.messageSource + "]");
			}
		}
		else {
			// Use empty MessageSource to be able to accept getMessage calls.
			DelegatingMessageSource dms = new DelegatingMessageSource();
			// 获取父容器的messageSource字段
			// 一般都为空
			dms.setParentMessageSource(getInternalParentMessageSource());
			this.messageSource = dms;
			// ----------该方法的核心---------------
			//  把MessageSource注入到容器中,以后获取国际化配置文件值的时候,可以自动注入MessageSource
			beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
			if (logger.isTraceEnabled()) {
				logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
			}
		}
	}
	// 取出国际化配置文件中的某个key值(可以按住区域信息获取)
	protected MessageSource getInternalParentMessageSource() {
		return (getParent() instanceof AbstractApplicationContext ?
				((AbstractApplicationContext) getParent()).messageSource : getParent());
	}

1.1 registerSingleton

	public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
		super.registerSingleton(beanName, singletonObject);
		updateManualSingletonNames(set -> set.add(beanName), set -> !this.beanDefinitionMap.containsKey(beanName));
		clearByTypeCache();
	}

You must first call the parent classregisterSingleton

	public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
		Assert.notNull(beanName, "Bean name must not be null");
		Assert.notNull(singletonObject, "Singleton object must not be null");
		synchronized (this.singletonObjects) {
			Object oldObject = this.singletonObjects.get(beanName);
			if (oldObject != null) {
				throw new IllegalStateException("Could not register object [" + singletonObject +
						"] under bean name '" + beanName + "': there is already object [" + oldObject + "] bound");
			}
			addSingleton(beanName, singletonObject);
		}
	}

The beanNameand singletonObjectadded to singletonObjects, and registeredSingletonsin and from singletonFactoriesand earlySingletonObjectsto remove the. It involves a method for storing Beanfour different of Map. It should be a simple explanation. In the creation of these Beanwill be frequently used time for resolve circular dependencies, caches and other issues.

  • singletonObjects: To save beanNameand beanrelationships between instances
  • singletonFactories: To save beanNameand create beanrelationships between plants
  • earlySingletonObjects: To save beanNameand beanrelationships between instances, the singletonObjectsdifference is that: when a single interest beanafter being placed there, then when beanstill in the process of creating species, it can getBeanobtain the method, purpose is to check cycle dependent .
  • registeredSingletons: To save all the current already created bean.
	protected void addSingleton(String beanName, Object singletonObject) {
		synchronized (this.singletonObjects) {
			this.singletonObjects.put(beanName, singletonObject);
			this.singletonFactories.remove(beanName);
			this.earlySingletonObjects.remove(beanName);
			this.registeredSingletons.add(beanName);
		}
	}

In the initMessageSourceimplementation of the latter, we look at beanFactorythe registeredSingletonsvalue of the property

Here Insert Picture Description

  • registeredSingletons[0]registeredSingletons[2]

    • In refresh> - prepareBeanFactoryregistration process
  • registeredSingletons[3]registeredSingletons[6]

    • In refresh> - invokeBeanFactoryPostProcessorsregistration process
  • registeredSingletons[7]registeredSingletons[10]

    • In refresh> - registerBeanPostProcessorsregistration process
  • registeredSingletons[11]

    • In refresh> - initMessageSourceregistration process

Second, the initApplicationEventMulticasterevent dispatcher

	protected void initApplicationEventMulticaster() {
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
			this.applicationEventMulticaster =
					beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
			if (logger.isTraceEnabled()) {
				logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
			}
		}
		else {
			this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
			beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
			if (logger.isTraceEnabled()) {
				logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
						"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
			}
		}
	}

With initMessageSourcesimilar methods, it not described in detail here. After a look at the implementation of the method beanFactoryof the registeredSingletonsvalue of the property of

Here Insert Picture Description
beanFactoryOf registeredSingletonsthe property more applicationEventMulticaster.

Next article we explain refreshthe ninth methods onRefreshand tenth methods registerListeners.

Published 397 original articles · won praise 71 · views 60000 +

Guess you like

Origin blog.csdn.net/xiaojie_570/article/details/104702604