/** * <p> * The root class from which all event state objects shall be derived. * <p> * All Events are constructed with a reference to the object, the "source", * that is logically deemed to be the object upon which the Event in question * initially occurred upon. * * @since JDK1.1 */
/** * The object on which the Event initially occurred. */ protectedtransient Object source;
/** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @exception IllegalArgumentException if source is null. */ publicEventObject(Object source){ if (source == null) thrownew IllegalArgumentException("null source");
this.source = source; }
/** * The object on which the Event initially occurred. * * @return The object on which the Event initially occurred. */ public Object getSource(){ return source; }
/** * Returns a String representation of this EventObject. * * @return A a String representation of this EventObject. */ public String toString(){ return getClass().getName() + "[source=" + source + "]"; } }
// 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) { // 从beanFactory中获取所有实现了对应接口的bean,并注册 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); } } }
protectedvoidpublishEvent(Object event, @Nullable ResolvableType eventType){ Assert.notNull(event, "Event must not be null");
// Decorate event as an ApplicationEvent if necessary 如果事件可以是ApplicationEvent,则强转 ApplicationEvent applicationEvent; if (event instanceof ApplicationEvent) { applicationEvent = (ApplicationEvent) event; } else { applicationEvent = new PayloadApplicationEvent<>(this, event); if (eventType == null) { // 这里的eventType平常使用的话是null eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType(); } }
// Multicast right now if possible - or lazily once the multicaster is initialized // 如果可能就立即广播,或者等待初始化 if (this.earlyApplicationEvents != null) { this.earlyApplicationEvents.add(applicationEvent); } else { // 可以看到就是获取了广播器,进行了事件的广播 getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType); }
// Publish event via parent context as well... 如果有父容器,那么也需要调用 if (this.parent != null) { if (this.parent instanceof AbstractApplicationContext) { ((AbstractApplicationContext) this.parent).publishEvent(event, eventType); } else { this.parent.publishEvent(event); } } }