当前位置:首页 > 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

开通会员,享受整站包年服务立即开通 >