返回顶部
首页 > 资讯 > 后端开发 > Python >ApplicationListenerDetector监听器判断demo
  • 849
分享到

ApplicationListenerDetector监听器判断demo

ApplicationListenerDetector监听器监听器判断 2023-03-14 14:03:39 849人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录Bean实例化之后Bean销毁之前Bean实例化之后 判断Bean是否是监听器,如果是监听器就将当前Bean加入监听器集合 public Object postProcessA

Bean实例化之后

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

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从监听器集合中移除

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监听器的资料请关注编程网其它相关文章!

--结束END--

本文标题: ApplicationListenerDetector监听器判断demo

本文链接: https://lsjlt.com/news/199769.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作