ApplicationListenerDetector監(jiān)聽器判斷demo
更新時間:2023年03月14日 14:43:52 作者:無名之輩J
這篇文章主要為大家介紹了ApplicationListenerDetector監(jiān)聽器判斷demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
Bean實例化之后
判斷Bean是否是監(jiān)聽器,如果是監(jiān)聽器就將當前Bean加入監(jiān)聽器集合
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof 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)) {
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
// inner bean with other scope - can't reliably process events
logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
"but is not reachable for event multicasting by its containing ApplicationContext " +
"because it does not have singleton scope. Only top-level listener beans are allowed " +
"to be of non-singleton scope.");
}
this.singletonNames.remove(beanName);
}
}
return bean;
}Bean銷毀之前
如果當前Bean是監(jiān)聽器,就將當前Bean從監(jiān)聽器集合中移除
public void postProcessBeforeDestruction(Object bean, String beanName) {
if (bean instanceof 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
}
}
}以上就是ApplicationListenerDetector監(jiān)聽器判斷demo的詳細內容,更多關于ApplicationListenerDetector監(jiān)聽器的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot如何統(tǒng)一處理返回結果和異常情況
這篇文章主要介紹了SpringBoot如何統(tǒng)一處理返回結果和異常情況問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
maven項目test執(zhí)行main找不到資源文件的問題及解決
這篇文章主要介紹了maven項目test執(zhí)行main找不到資源文件的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot中支持Https協(xié)議的實現(xiàn)
本文主要介紹了SpringBoot中支持Https協(xié)議的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01

