//EventListener package java.util;/**
* A tagging interface that all event listener interfaces must extend.
* @since JDK1.1
*/publicinterfaceEventListener{}//ApplicationListener@FunctionalInterfacepublicinterfaceApplicationListener<E extendsApplicationEvent>extendsEventListener{/**
* Handle an application event.
* @param event the event to respond to
*/voidonApplicationEvent(E event);/**
* Create a new {@code ApplicationListener} for the given payload consumer.
* @param consumer the event payload consumer
* @param <T> the type of the event payload
* @return a corresponding {@code ApplicationListener} instance
* @since 5.3
* @see PayloadApplicationEvent
*/static<T> ApplicationListener<PayloadApplicationEvent<T>>forPayload(Consumer<T> consumer){return event -> consumer.accept(event.getPayload());}}
系统广播器定义
publicinterfaceApplicationEventMulticaster{/**
* Add a listener to be notified of all events.
* @param listener the listener to add
*/voidaddApplicationListener(ApplicationListener<?> listener);/**
* Add a listener bean to be notified of all events.
* @param listenerBeanName the name of the listener bean to add
*/voidaddApplicationListenerBean(String listenerBeanName);/**
* Remove a listener from the notification list.
* @param listener the listener to remove
*/voidremoveApplicationListener(ApplicationListener<?> listener);/**
* Remove a listener bean from the notification list.
* @param listenerBeanName the name of the listener bean to remove
*/voidremoveApplicationListenerBean(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.
*/voidremoveAllListeners();/**
* 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.
* @param event the event to multicast
*/voidmulticastEvent(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.
* @param event the event to multicast
* @param eventType the type of event (can be {@code null})
* @since 4.2
*/voidmulticastEvent(ApplicationEvent event,@Nullable ResolvableType eventType);}
系统事件
监听器注册
protectedvoidregisterListeners(){// 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);}}}