SpringApplicationListener是Spring为我们提供的事件监听器,它主要是为我们提供事件监听机制,我们可以创建一个监听器,并向该监听器创建相应的事件,当该事件发生时,我们的监听器就会得到相应的通知,并且触发相应的逻辑。是典型的观察者模式
事件监听器是Spring为我们定义的一个接口,该接口所携带的泛型就是我们要监听的事件,我们来实现一个ApplicationListener接口
@Component public class MyApplicationListener implements ApplicationListener<ApplicationEvent> { //当容器中发布此事件以后,方法触发 @Override public void onApplicationEvent(ApplicationEvent event) { System.out.println("reveice event: " + event); } }Spring的ApplicationContext为我们提供了一个方法publishEvent()该方法可以为我们发布事件。
applicationContext.publishEvent(new ApplicationEvent(new Stirng("我发布的事件")));ServletWebServerInitializedEvent的接收
在Spring refresh容器后,会执行一个finishRefresh()方法。
getLifecycleProcessor().onRefresh();
this.applicationContext.publishEvent(new ServletWebServerInitializedEvent(this.webServer,this.applicationContext));
getApplicationEventMulticaster().multicastEvent(applicationEvent,eventType);
//拿到所有符合条件的ApplicationListener,遍历它 for (ApplicationListener<?> listener : getApplicationListeners(event, type)) { if (executor != null) { executor.execute(() -> invokeListener(listener, event)); } else { //依次调用每个ApplicationListener的onApplicationEvent方法 invokeListener(listener, event); } }其他Event的发布和上述流程基本相符。
关于applicationEventMulticaster的注入
protected void initApplicationEventMulticaster() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); //如果容器中有APPLICATION_EVENT_MULTICASTER_BEAN_NAME,则获取该组件,并加入到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 { //如果容器中没有如果容器中有APPLICATION_EVENT_MULTICASTER_BEAN_NAME,就自己创建一个单实例bean,注入到beanFactory中 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() + "]"); } } }容器中有哪些监听器?
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); } } }