當(dāng)前位置:首頁 > IT技術(shù) > 移動(dòng)平臺(tái) > 正文

ApplicationListenerDetector
2021-09-13 10:35:35

ApplicationListenerDetector

處理用戶自定義ApplicationListener注冊(cè)和銷毀

類圖

ApplicationListenerDetector_事件監(jiān)聽器

postProcessAfterInitialization

public Object postProcessAfterInitialization(Object bean, String beanName) {
		if (bean instanceof ApplicationListener) { // 是否ApplicationListener類型
			// potentially not detected as a listener by getBeanNamesForType retrieval
			Boolean flag = this.singletonNames.get(beanName); // 是否單例標(biāo)志
			if (Boolean.TRUE.equals(flag)) {
				// singleton bean (top-level or inner): register on the fly
				this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); // 單例就注冊(cè)事件監(jiān)聽器
			}
			else if (Boolean.FALSE.equals(flag)) {
				this.singletonNames.remove(beanName); // 非單例 刪除標(biāo)志
			}
		}
		return bean;
	}

postProcessBeforeDestruction

@Override // 銷毀 destroy()方法調(diào)用
	public void postProcessBeforeDestruction(Object bean, String beanName) {
		if (bean instanceof ApplicationListener) { // 是否ApplicationListener類型
			try {
				ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
				multicaster.removeApplicationListener((ApplicationListener<?>) bean); // 刪除該注冊(cè)事件監(jiān)聽器
				multicaster.removeApplicationListenerBean(beanName);
			}
			catch (IllegalStateException ex) {
				// ApplicationEventMulticaster not initialized yet - no need to remove a listener
			}
		}
	}

本文摘自 :https://blog.51cto.com/u

開通會(huì)員,享受整站包年服務(wù)立即開通 >