国际化与事件的初始化
1. 前言
1. 前言
Spring的容器叫ApplicationContext,他的本质其实就是一个BeanFactory,那它和BeanFactory有哪些主要的不同之处呢?这就涉及到本文介绍的重点:国际化与事件,当然除了这两个特性ApplicationContext还有很多其它特性。本文重点介绍Spring容器启动过程中的国际化与事件两个功能的初始化,该环节在整个Spring容器启动过程中的位置如图中的红色方框部分所示。
本文涉及到的Spring Refresh阶段的部分源码如下:
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
2. Spring国际化初始化
国际化一般用于应用的语言切换,比如一个系统需要给不同的人展示不同的语言,但一个中国人登录之后展示中文,英国人登录之后展示英文,这就是典型的i18n问题。在Spring的i18n系统中,用户需要为每种语言提供一套相应的资源文件,并以规范化命名的方式保存在特定的目录中,由系统自动根据客户端语言选择适合的资源文件。Spring提供了MessageSource
接口用于解决i18n问题,并且提供了一个允许设置父子关系的接口HierarchicalMessageSource
,这些接口和实现共同构成了Spring的国际化消息的基础。MessageSource
接口主要包含以下方法:
String getMessage(String code, Object[] args, String default, Locale loc)
: 根据消息的标识(code),获取指定语言(loc)对应的消息,如果没有查找到对应的消息,那么就使用默认值(default),消息中允许传递参数。String getMessage(String code, Object[] args, Locale loc)
: 根据消息的标识(code),获取指定语言(loc)对应的消息,如果没有查找到对应的消息,抛出异常。String getMessage(MessageSourceResolvable resolvable, Locale locale):
从指定的resolvable中查找指定语言的消息。
public interface MessageSource {
/**
* Try to resolve the message. Return default message if no message was found.
* @param code the message code to look up, e.g. 'calculator.noRateSet'.
* MessageSource users are encouraged to base message names on qualified class
* or package names, avoiding potential conflicts and ensuring maximum clarity.
*/
@Nullable
String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
/**
* Try to resolve the message. Treat as an error if the message can't be found.
* @param code the message code to look up, e.g. 'calculator.noRateSet'.
* MessageSource users are encouraged to base message names on qualified class
* or package names, avoiding potential conflicts and ensuring maximum clarity.
* @param args an array of arguments that will be filled in for params within
* the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
* or {@code null} if none
*/
String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
/**
* Try to resolve the message using all the attributes contained within the
* {@code MessageSourceResolvable} argument that was passed in.
* <p>NOTE: We must throw a {@code NoSuchMessageException} on this method
* since at the time of calling this method we aren't able to determine if the
* {@code defaultMessage} property of the resolvable is {@code null} or not.
*/
String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
}
2.1 用户自定义MessageSource初始化
Spring允许用户自定义实现MessageSource接口,但是用户自定义MessageSource组件的名称必须为"messageSource"
。Spring容器在启动的过程中会去容器中检查是否包含指定名称的Bean或者Bean定义,如果已经MessageSource的Bean定义,那么就使用该Bean定义。
如果用户自定义的MessageSource实现了HierarchicalMessageSource接口,而且当前Spring容器有父容器,那么Spring会把父容器的MessageSource设置为当前容器MessageSource的父MessageSource。
2.2 Spring默认的MessageSource初始化
如果用户没有自定义MessageSource组件,那么Spring会使用默认的MessageSource实现方案:DelegatingMessageSource,DelegatingMessageSource只是父容器MessageSource的一个代理,如果没有父容器或者父容器的MessageSource为null,那么此处可能会抛出异常。所以如果容器需要i18n功能的话,用户一定要自定义MessageSource(Spring提供了多种开箱即用的MessageSource如ResourceBundleMessageSource等)
Spring 初始化MessageSource的源码如下:
/**
* Initialize the MessageSource.
* Use parent's if none defined in this context.
*/
protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
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();
dms.setParentMessageSource(getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isTraceEnabled()) {
logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
}
}
}
3. 应用事件广播初始化
Spring容器支持ApplicationEvent事件通知机制,当应用发布对应的ApplicationEvent之后,容器会通知对应的ApplicationListener去处理这些事件,那么容器通过谁来管理以及通知这些ApplicationListener呢?这就是本节的重点:ApplicationEventMulticaster
事件广播器,该广播器有两个功能:管理ApplicationListener和广播ApplicationEvent。接口的定义如下:
public interface ApplicationEventMulticaster {
/**
* Add a listener to be notified of all events.
*/
void addApplicationListener(ApplicationListener<?> listener);
/**
* Add a listener bean to be notified of all events.
*/
void addApplicationListenerBean(String listenerBeanName);
/**
* Remove a listener from the notification list.
*/
void removeApplicationListener(ApplicationListener<?> listener);
/**
* Remove a listener bean from the notification list.
* @param listenerBeanName the name of the listener bean to remove
*/
void removeApplicationListenerBean(String listenerBeanName);
/**
* Remove all listeners registered with this multicaster.
* <p>After a remove call, the multicaster will perform no action
* on event notification until new listeners are registered.
*/
void removeAllListeners();
/**
* Multicast the given application event to appropriate listeners.
* <p>Consider using {@link #multicastEvent(ApplicationEvent, ResolvableType)}
* if possible as it provides better support for generics-based events.
*/
void multicastEvent(ApplicationEvent event);
/**
* Multicast the given application event to appropriate listeners.
* <p>If the {@code eventType} is {@code null}, a default type is built
* based on the {@code event} instance.
* @since 4.2
*/
void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType);
}
3.1 用户自定义广播初始化
Spring允许用户自定义事件广播,但是广播的名称必须为"applicationEventMulticaster"
,在初始化阶段,Spring容器会实例化该广播并注册到容器中。
3.2 Spring默认广播的初始化
如果用户没有自定义广播事件,那么Spring会使用默认的事件官博SimpleApplicationEventMulticaster
,该广播支持同步广播和异步广播两种方式,用户可以按需求设置
Spring 初始化ApplicationEventMulticaster的源码如下:
/**
* Initialize the ApplicationEventMulticaster.
* Uses SimpleApplicationEventMulticaster if none defined in the context.
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
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() + "]");
}
}
}
4 事件Listener的注册
事件广播实例化之后,需要向其中注册容器中定义好的ApplicationListener,这些ApplicationListener分为两大类,一类是容器启动阶段就实例化好的 ApplicationListener,另外一类是用户自定义还没实例化好的ApplicationListener。在初始化阶段,Spring会把这些Listener 都添加到ApplicationEventMulticaster中。该阶段的源码如下:
/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
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 (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
我是御狐神,欢迎大家关注我的微信公众号