1、initMessageSource()

Spring对MessageSource的初始化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//初始化MessageSource,如果在此上下文中没有定义父类,则使用父类。
protected void initMessageSource() {
//获取beanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//如果本地上下文中存在messageSource
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
// 让MessageSource知道父消息源。
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
if (hms.getParentMessageSource() == null) {
// 如果没有已注册的父消息源,则只将父上下文设置为父消息源。
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger.isTraceEnabled()) {
logger.trace("Using MessageSource [" + this.messageSource + "]");
}
} else {
// 使用空MessageSource能够接受getMessage调用。
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 + "]");
}
}
}

2、initApplicationEventMulticaster()

Spring对事件监听器的管理器的初始化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//applicationEventMulticaster
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() + "]");
}
}
}

3、onRefresh()

1
2
3
protected void onRefresh() throws BeansException {
// For subclasses: do nothing by default.
}

4、registerListeners()

为上面定义的事件监听器的管理器注册监听。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//添加实现ApplicationListener作为侦听器的bean。不影响其他侦听器,可以在不添加bean的情况下添加。
protected void registerListeners() {
// 首先注册静态指定的侦听器。
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}

// 不要在这里初始化factorybean:我们需要不初始化所有常规bean,让后处理程序应用于它们!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}

// 发布早期的应用程序事件,现在我们终于有一个多播…
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}

5、ApplicationEventMulticaster与MessageSource接口的作用

5.1、MessageSource

用于解析消息的策略接口,支持此类消息的参数化和国际化。有两个开箱即用的实现:

  • org.springframework.context.support.ResourceBundleMessageSource
  • org.springframework.context.support.ReloadableResourceBundleMessageSource。

5.2、ApplicationEventMulticaster

接口由能够管理多个ApplicationListener对象并向其发布事件的对象实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
//添加一个侦听器来通知所有事件。
void addApplicationListener(ApplicationListener<?> listener);
//添加一个侦听器bean来通知所有事件。
void addApplicationListenerBean(String listenerBeanName);
//从通知列表中删除侦听器。
void removeApplicationListener(ApplicationListener<?> listener);
//从通知列表中删除侦听器bean。
void removeApplicationListenerBean(String listenerBeanName);
void removeAllListeners();
//将给定的应用程序事件多路广播给匹配的侦听器。如果可能,考虑使用{@link#multicastEvent(ApplicationEvent,ResolvableType)},因为它为基于泛型的事件提供更好的支持。
void multicastEvent(ApplicationEvent event);
//将给定的应用程序事件多路广播给匹配的侦听器。如果{@code eventType}为空,会构建一个基于{@code event}实例的类型。
void multicastEvent(ApplicationEvent event, ResolvableType eventType);

tencent.jpg