1.SpringApplicationRunListeners和SpringApplicationRunListener对视springboot中新增的类和接口。
2.SpringApplicationRunListeners是一个封装类,主要对SpringApplicationRunListener的各个操作进行封装。
3.EventPublishingRunListener实现了SpringApplicationRunListener,作为SimpleApplicationEventMulticaster的封装类,在SpringApplication.run(CommonsTestApplication.class, args)的run方法的
SpringApplicationRunListeners listeners = getRunListeners(args)中获取到的就是EventPublishingRunListener,然后再启动的各个阶段进行事件的分发。
4.SimpleApplicationEventMulticaster是实际上对ApplicationListener监听事件进行分发的类。
SimpleApplicationEventMulticaster实际进行事件分发
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
listener.onApplicationEvent(event);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled()) {
logger.debug("Non-matching event type for listener: " + listener, ex);
}
}
else {
throw ex;
}
}
}