瞧瞧官方注释
/**
* Initialize the MessageSource.
* Use parent's if none defined in this context.
*/
初始化MessageSource消息源,如果beanFactory不存在此bean则采用默认的配置并设置父类MessageSource
直接源码
protected void initMessageSource() {
//获取bean工厂,此处一般为DefaultListableBeanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//首先判断是否已有xml文件定义了id为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的实现类
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
//设置父类MessageSource,此处设置内部的parent messageSource
if (hms.getParentMessageSource() == null) {
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger.isDebugEnabled()) {
logger.debug("Using MessageSource [" + this.messageSource + "]");
}
}
else {
// 则设置默认的DelegatingMessageSource 作为messageSource bean
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 + "]");
}
}
}