ApplicationEventPublisher是ApplicationContext的父接口之一。這接口的作用是:Interface that encapsulates event publication functionality.
功能就是發布事件,也就是把某個事件告訴的所有與這個事件相關的監聽器。
這個接口只有兩個方法(這兩個是重載方法)。所以把整個接口的代碼都貼上來。
/** * Interface that encapsulates event publication functionality. * Serves as super-interface for {@link ApplicationContext}. * * @author Juergen Hoeller * @author Stephane Nicoll * @since 1.1.1 * @see ApplicationContext * @see ApplicationEventPublisherAware * @see org.springframework.context.ApplicationEvent * @see org.springframework.context.event.EventPublicationInterceptor */ public interface ApplicationEventPublisher { /** * Notify all <strong>matching</strong> listeners registered with this * application of an application event. Events may be framework events * (such as RequestHandledEvent) or application-specific events. * @param event the event to publish * @see org.springframework.web.context.support.RequestHandledEvent
*/ void publishEvent(ApplicationEvent event); /** * Notify all <strong>matching</strong> listeners registered with this * application of an event. * <p>If the specified {@code event} is not an {@link ApplicationEvent}, * it is wrapped in a {@link PayloadApplicationEvent}. * @param event the event to publish * @since 4.2 * @see PayloadApplicationEvent */ void publishEvent(Object event); }
這個方法會通知的所有與事件相匹配的監聽器。這些監聽可能是spring框架的監聽器,也有可能是特定的監聽器。