protectedvoidregisterListeners(){// Register statically specified listeners first.//获取容器中事件监听并存放到多播器中 applicationListenersfor(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);//从容器中获取事件监听的beanName 存放到多播器中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);}}}
privatevoiddoInvokeListener(ApplicationListener listener,ApplicationEvent event){try{//调用监听器的onApplicationEvent方法执行
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.isTraceEnabled()){
logger.trace("Non-matching event type for listener: "+ listener, ex);}}else{throw ex;}}}