ApplicationListenerDetector监听器判断demo

 更新时间:2023年03月14日 14:43:52   作者:无名之辈J  
这篇文章主要为大家介绍了ApplicationListenerDetector监听器判断demo,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

Java技术迷

Bean实例化之后

判断Bean是否是监听器,如果是监听器就将当前Bean加入监听器集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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是监听器,就将当前Bean从监听器集合中移除

1
2
3
4
5
6
7
8
9
10
11
12
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监听器判断demo的详细内容,更多关于ApplicationListenerDetector监听器的资料请关注脚本之家其它相关文章!

蓄力AI

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

原文链接:https://juejin.cn/post/7204712464642981925/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!

相关文章

最新评论