ApplicationListenerDetector
處理用戶自定義ApplicationListener注冊和銷毀
類圖
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); // 單例就注冊事件監(jiān)聽器
}
else if (Boolean.FALSE.equals(flag)) {
this.singletonNames.remove(beanName); // 非單例 刪除標志
}
}
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); // 刪除該注冊事件監(jiān)聽器
multicaster.removeApplicationListenerBean(beanName);
}
catch (IllegalStateException ex) {
// ApplicationEventMulticaster not initialized yet - no need to remove a listener
}
}
}
本文摘自 :https://blog.51cto.com/u