• 當前位置:首頁 > IT技術 > 移動平臺 > 正文

    ApplicationListenerDetector
    2021-09-13 10:35:35

    ApplicationListenerDetector

    處理用戶自定義ApplicationListener注冊和銷毀

    類圖

    ApplicationListenerDetector_事件監聽器

    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); // 是否單例標志
    			if (Boolean.TRUE.equals(flag)) {
    				// singleton bean (top-level or inner): register on the fly
    				this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); // 單例就注冊事件監聽器
    			}
    			else if (Boolean.FALSE.equals(flag)) {
    				this.singletonNames.remove(beanName); // 非單例 刪除標志
    			}
    		}
    		return bean;
    	}
    

    postProcessBeforeDestruction

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

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

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码