//获取bean工厂
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//bean工厂是否包含bean——messageSource
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
//本对象的messageSource=根据bean名(messageSource)反射出来的bean
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
// Make messageSource aware of parent MessageSource.
//如果本对象的application不为null且本对象的messageSource属于HierarchicalMessageSource类型
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
//本对象的messagesource强转为HierarchicalMessageSource
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
//如果hms的父级messageSource=null
if (hms.getParentMessageSource() == null) {
// Only set parent context as parent MessageSource if no parent MessageSource
// registered already.
//如果context有父级则返回父级,没有则返回自身。把context丢到dms的ParentMessageSource中
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger.isTraceEnabled()) {
logger.trace(“Using MessageSource [” + this.messageSource + “]”);
}
}
//bean工厂未包含bean
else {
// Use empty MessageSource to be able to accept getMessage calls.
//创建一个空的messageSource
DelegatingMessageSource dms = new DelegatingMessageSource();
//如果context有父级则返回父级,没有则返回自身。把context丢到dms的ParentMessageSource中
dms.setParentMessageSource(getInternalParentMessageSource());
//本对象的messageSource=ParentMessageSource
this.messageSource = dms;
//注册bean
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isTraceEnabled()) {
logger.trace(“No '” + MESSAGE_SOURCE_BEAN_NAME + “’ bean, using [” + this.messageSource + “]”);
}
}
spring源码——initMessageSource(初始化消息源)
最新推荐文章于 2024-05-20 21:12:10 发布