首頁  >  文章  >  Java  >  SpringBoot監聽器模式怎麼實現

SpringBoot監聽器模式怎麼實現

WBOY
WBOY轉載
2023-05-20 19:07:19914瀏覽

我們來以應用程式啟動事件:ApplicationStartingEvent為例來進行說明:

以啟動類別的SpringApplication.run方法為入口,跟進SpringApplication的兩個同名方法後,我們會看到主要的run方法,方法比較長,在這裡只貼出與監聽器密切相關的關鍵的部分:

SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();

我們跟進這個starting方法,方法的內容如下:

void starting() {
	for (SpringApplicationRunListener listener : this.listeners) {
		listener.starting();
	}
}

這裡的listeners已經在getRunListeners方法中完成了加載,加載原理類似於系統初始化器,關於系統初始化器的加載可以參考SpringBoot深入淺出分析初始化器

starting方法邏輯很簡單,就是調用SpringApplicationRunListener的starting方法。下面繼續分析這個starting方法:

我們進入了EventPublishingRunListener類別(SpringApplicationRunListener 的實作類別)的starting方法:

	@Override
	public void starting() {
		this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
	}

這裡就使用了廣播器,來廣播新的ApplicationStartingEvent事件。

我們跟進這個multicastEvent方法:

	@Override
	public void multicastEvent(ApplicationEvent event) {
		multicastEvent(event, resolveDefaultEventType(event));
	}

繼續看同名的方法multicastEvent:

	@Override
	public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
		ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
		Executor executor = getTaskExecutor();
		for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
			if (executor != null) {
				executor.execute(() -> invokeListener(listener, event));
			}
			else {
				invokeListener(listener, event);
			}
		}
	}

這裡的ResolvableType 是對event做了包裝,我們不去關注;由於我們沒有創建線程池,所以executor是空的。我們重點關注兩個部分:

取得所有監聽此事件的應用程式監聽器

2、invokeListener --> 啟動監聽器;

getApplicationListeners (AbstractApplicationEventMulticasterster類別中)方法,程式碼如下:

	protected Collection<ApplicationListener<?>> getApplicationListeners(
			ApplicationEvent event, ResolvableType eventType) {
		Object source = event.getSource();
		Class<?> sourceType = (source != null ? source.getClass() : null);
		ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);
		// Quick check for existing entry on ConcurrentHashMap...
		ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
		if (retriever != null) {
			return retriever.getApplicationListeners();
		}
		if (this.beanClassLoader == null ||
				(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
						(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
			// Fully synchronized building and caching of a ListenerRetriever
			synchronized (this.retrievalMutex) {
				retriever = this.retrieverCache.get(cacheKey);
				if (retriever != null) {
					return retriever.getApplicationListeners();
				}
				retriever = new ListenerRetriever(true);
				Collection<ApplicationListener<?>> listeners =
						retrieveApplicationListeners(eventType, sourceType, retriever);
				this.retrieverCache.put(cacheKey, retriever);
				return listeners;
			}
		}
		else {
			// No ListenerRetriever caching -> no synchronization necessary
			return retrieveApplicationListeners(eventType, sourceType, null);
		}
	}

入參中的event是ApplicationStartingEvent,sourceType是org.springframework.boot.SpringApplication類別。我把ListenerRetriever類型看成一個儲存監聽器的容器。

可以看出,程式先在快取裡面尋找ListenerRetriever類型的retriever,如果沒有找到,加鎖再從快取裡面找一次。這裡我們快取裡是沒有內容的,所以都不會回來。

然後使用retrieveApplicationListeners方法,對監聽器進行遍歷。 retrieveApplicationListeners方法比較長,我們將重點放在下supportsEvent(listener, eventType, sourceType)方法,該方法用來判斷是否此監聽器關注該事件,過程主要包括,判斷此類型是否為GenericApplicationListener類型,如果不是,則建構一個代理,代理的目的是,透過泛型解析,最終獲得監聽器所感興趣的事件。

如果經過判斷,監聽器對該事件是感興趣的,則此監聽器會被加入監聽器清單中。

	protected boolean supportsEvent(
			ApplicationListener<?> listener, ResolvableType eventType, @Nullable Class<?> sourceType) {
		GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener ?
				(GenericApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
		return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
	}

當某個事件所有的監聽器被收集完畢後,multicastEvent(SimpleApplicationEventMulticaster類別)方法就會對事件進行傳播。也就是呼叫監聽器的通用觸發介面方法:listener.onApplicationEvent(event);這樣,就完成了這個事件的傳播。

以上是SpringBoot監聽器模式怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除