Previous: Refresh the Spring-12 is the parse source (5) - registerBeanPostProcessors
Previous analysis we finished registerBeanPostProcessors
, then we begin to analyze refresh
the methods of the seventh initMessageSource
and eighth methodsinitApplicationEventMulticaster
One,initMessageSource
Initialization MessageSource
component (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 beanName
and singletonObject
added to singletonObjects
, and registeredSingletons
in and from singletonFactories
and earlySingletonObjects
to remove the. It involves a method for storing Bean
four different of Map
. It should be a simple explanation. In the creation of these Bean
will be frequently used time for resolve circular dependencies, caches and other issues.
singletonObjects
: To savebeanName
andbean
relationships between instancessingletonFactories
: To savebeanName
and createbean
relationships between plantsearlySingletonObjects
: To savebeanName
andbean
relationships between instances, thesingletonObjects
difference is that: when a single interestbean
after being placed there, then whenbean
still in the process of creating species, it cangetBean
obtain the method, purpose is to check cycle dependent .registeredSingletons
: To save all the current already createdbean
.
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 initMessageSource
implementation of the latter, we look at beanFactory
the registeredSingletons
value of the property
-
registeredSingletons[0]
–registeredSingletons[2]
- In
refresh
> -prepareBeanFactory
registration process
- In
-
registeredSingletons[3]
–registeredSingletons[6]
- In
refresh
> -invokeBeanFactoryPostProcessors
registration process
- In
-
registeredSingletons[7]
–registeredSingletons[10]
- In
refresh
> -registerBeanPostProcessors
registration process
- In
-
registeredSingletons[11]
- In
refresh
> -initMessageSource
registration process
- In
Second, the initApplicationEventMulticaster
event 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 initMessageSource
similar methods, it not described in detail here. After a look at the implementation of the method beanFactory
of the registeredSingletons
value of the property of
beanFactory
Of registeredSingletons
the property more applicationEventMulticaster
.
Next article we explain refresh
the ninth methods onRefresh
and tenth methods registerListeners
.