大家對(duì)ApplicationListener應(yīng)該不會(huì)陌生,但是大家是否了解Spring事件監(jiān)聽(tīng)機(jī)制是怎么實(shí)現(xiàn)的呢掌唾?讓我們一起來(lái)看源碼糯彬!
Spring的事件監(jiān)聽(tīng)機(jī)制是觀察者模式的一個(gè)典型應(yīng)用撩扒,觀察者模式試圖定義對(duì)象間的一種一對(duì)多的依賴關(guān)系,當(dāng)一個(gè)對(duì)象的狀態(tài)發(fā)生改變時(shí)炒辉,所有依賴于它的對(duì)象都得到通知并被自動(dòng)更新黔寇。
首先我們明確事件監(jiān)聽(tīng)的三個(gè)要素:事件缝裤、事件監(jiān)聽(tīng)器憋飞、事件源。
在spring的事件監(jiān)聽(tīng)機(jī)制中猾编,ApplicationEvent充當(dāng)事件的角色答倡。所有的事件都要繼承ApplicationEvent瘪撇。
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened. */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
ApplicationListener充當(dāng)事件監(jiān)聽(tīng)器的角色,當(dāng)事件發(fā)生時(shí)鹏氧,進(jìn)行對(duì)應(yīng)的操作实蓬。它對(duì)應(yīng)著觀察者模式中的觀察者Observer。當(dāng)監(jiān)聽(tīng)的事件發(fā)生后該方法會(huì)被執(zhí)行调鬓。
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
事件源是事件發(fā)起的地方腾窝,ApplicationEventPublisher接口下的方法就是發(fā)布事件的虹脯,實(shí)現(xiàn)該接口便可以作為事件源归形,AbstractApplicationContext實(shí)現(xiàn)了該接口鼻由,在finishRefresh()方法中就會(huì)調(diào)用publishEvent()方法發(fā)布事件蔼紧。
@FunctionalInterface
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
*/
default void publishEvent(ApplicationEvent event) {
publishEvent((Object) 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);
}
從一個(gè)簡(jiǎn)單的例子開(kāi)始分析源碼
定義一個(gè)MyConfigApplicationContext繼承與AnnotationConfigApplicationContext
public class MyConfigApplicationContext extends AnnotationConfigApplicationContext {
public MyConfigApplicationContext(Class c) {
super(c);
}
@Override
protected void onRefresh() throws BeansException {
this.publishEvent(new ApplicationEvent("我手動(dòng)發(fā)布了一個(gè)事件") {
@Override
public Object getSource() {
return super.getSource();
}
});
super.onRefresh();
}
}
我們先不討論為何要這樣寫,繼續(xù)寫配置類
@Configuration
@ComponentScan(basePackages = {"com.example.demo"})
public class MainConfig {
}
然后主類
public class DemoApplication {
public static void main(String[] args) {
MyConfigApplicationContext ctx = new MyConfigApplicationContext(MainConfig.class);
}
}
運(yùn)行一下
順著這個(gè)跟蹤一下源碼
可以看到在創(chuàng)建自定義的bean之前監(jiān)聽(tīng)器已經(jīng)監(jiān)聽(tīng)到事件發(fā)生了,所以我們從ApplicationContext的構(gòu)造器開(kāi)始看起
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
//創(chuàng)建關(guān)鍵組件
this();
//注冊(cè)配置類MainConfig的bean定義(BeanDefinition)逻卖,此時(shí)還未實(shí)例化bean
register(annotatedClasses);
refresh();
}
重點(diǎn)看refresh()方法
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
可以看到initApplicationEventMulticaster()方法,看這名字就覺(jué)得跟事件監(jiān)聽(tīng)的有關(guān)盗迟,該方法初始化事件廣播器罚缕。
protected void initApplicationEventMulticaster() {
//先獲取beanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//看看是否有applicationEventMulticaster這個(gè)bean存在
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 {
//創(chuàng)建一個(gè)SimpleApplicationEventMulticaster喂饥,并注冊(cè)到容器中
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() + "]");
}
}
}
然后看registerListeners()方法,明顯也跟事件監(jiān)聽(tīng)有關(guān)
protected void registerListeners() {
//去容器中把a(bǔ)pplicationListener 撈取出來(lái)注冊(cè)到廣播器上去(系統(tǒng)的)
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
//注冊(cè)我們自己實(shí)現(xiàn)了ApplicationListener 的組件
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// 發(fā)布早期事件(防止某些事件出現(xiàn)在廣播器還沒(méi)有初始化的時(shí)候捞高,漏掉該部分事件)
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
//廣播器廣播早期事件
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
容器在registerListeners()方法之前publishEvent的都是早期事件硝岗,所以我們重寫了onRefresh()方法型檀,并在其中發(fā)布了一個(gè)事件胀溺,該事件為早期事件仓坞,然后在registerListeners時(shí)无埃,被廣播器廣播到監(jiān)聽(tīng)器嫉称。
finishRefresh()方法中有發(fā)布事件织阅。
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
clearResourceCaches();
// Initialize lifecycle processor for this context.
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh();
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
Assert.notNull(event, "Event must not be null");
// Decorate event as an ApplicationEvent if necessary
ApplicationEvent applicationEvent;
//支持兩種類型的事件
//1氮趋、直接繼承ApplicationEven
//2剩胁、其他事件晾腔,會(huì)被包裝為PayloadApplicationEvent,可以使用getPayload獲取真實(shí)的通知內(nèi)容
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
}
}
// 如果earlyApplicationEvents不為空壁查,便把事件加入到早期事件集合中
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
//廣播事件
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
// 如果設(shè)置了父容器,父容器同樣發(fā)布事件
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}
那什么時(shí)候earlyApplicationEvents不為空剔应,什么時(shí)候?yàn)榭漳兀?/p>
可以看到在refresh()方法執(zhí)行perpareRefresh()方法時(shí)睡腿,實(shí)例化earlyApplicationEvents集合,所以在此方法執(zhí)行后發(fā)布的事件會(huì)被加入早期事件集合峻贮,到執(zhí)行registerListeners方法席怪,該方法會(huì)廣播早期事件,而且把earlyApplicationEvents設(shè)置為null纤控,所以在registerListeners后發(fā)布的事件不是早期事件挂捻,廣播器直接廣播。
看下SimpleApplicationEventMulticaster.multicastEvent方法船万,看看其是如何廣播的
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
//獲取事件類型疫赎,如:org.springframework.boot.context.event.ApplicationStartingEvent
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
//獲取監(jiān)聽(tīng)該事件的監(jiān)聽(tīng)器狮荔,并循環(huán)每個(gè)監(jiān)聽(tīng)器
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
//是否異步執(zhí)行
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
multicastEvent()方法里面調(diào)用了invokeListener()
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
ErrorHandler errorHandler = getErrorHandler();
//是否設(shè)置了錯(cuò)誤處理器
if (errorHandler != null) {
try {
doInvokeListener(listener, event);
}
catch (Throwable err) {
errorHandler.handleError(err);
}
}
else {
doInvokeListener(listener, event);
}
}
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
//可以看到回調(diào)了監(jiān)聽(tīng)器的onApplicationEvent()方法爵憎,典型的觀察者設(shè)計(jì)模式
//廣播器持有監(jiān)聽(tīng)器(觀察者)巴刻,然后在事件發(fā)生時(shí)碍舍,廣播器回調(diào)監(jiān)聽(tīng)器的方法
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;
}
}
}
而廣播器廣播其實(shí)就是觀察者模式,廣播器持有監(jiān)聽(tīng)器,在廣播時(shí)回調(diào)監(jiān)聽(tīng)器的onApplicationEvent()方法了嚎。過(guò)程如下圖:
這就是spring事件監(jiān)聽(tīng)機(jī)制的大致流程慎式。
可以看到初始化廣播器的時(shí)候,會(huì)先從容器中獲取 applicationEventMulticaster 的 bean蕾盯。
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
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 {
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() + "]");
}
}
}
由此可見(jiàn)我們可以自己注入一個(gè) applicationEventMulticaster bean。例如:
@Configuration
@ComponentScan(basePackages = {"com.example.demo"})
public class MainConfig {
@Autowired
private Executor syncTaskExecutor;
@Bean("applicationEventMulticaster")
public AbstractApplicationEventMulticaster applicationEventMulticaster() {
SimpleApplicationEventMulticaster abstractApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
abstractApplicationEventMulticaster.setTaskExecutor(syncTaskExecutor);
return abstractApplicationEventMulticaster;
}
}
這樣監(jiān)聽(tīng)器就會(huì)異步處理事件锌雀。
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
ErrorHandler errorHandler = getErrorHandler();
if (errorHandler != null) {
try {
doInvokeListener(listener, event);
}
catch (Throwable err) {
errorHandler.handleError(err);
}
}
else {
doInvokeListener(listener, event);
}
}