Bean相关的处理器 二、ApplicationListenerDetector

ApplicationListenerDetector在Spring Boot中扮演关键角色,它在Bean初始化后若为单例且实现ApplicationListener接口,则将其添加到事件监听列表。同时,在Bean销毁前,它会从ApplicationEventMulticaster中移除该监听器,确保资源的有效管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 ApplicationListenerDetector:主要作用:

    1、在Bean初始化完成之后:如果Bean是单例的则并且bean instanceof ApplicationListener。加入到this.applicationListeners中。

    2、在Bean销毁之前搞事情: 如果Bean是一个ApplicationListener,则会从ApplicationEventMulticaster(事件广播器)中提前删除了。

class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor {

   private static final Log logger = LogFactory.getLog(ApplicationListenerDetector.class);
   private final transient AbstractApplicationContext applicationContext;
   private final transient Map<String, Boolean> singletonNames = new ConcurrentHashMap<>(256);
   public ApplicationListenerDetector(AbstractApplicationContext applicationContext) {
      this.applicationContext = applicationContext;
   }

   @Override
   public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
      this.singletonNames.put(beanName, beanDefinition.isSingleton());
   }
    
   @Override
   public Object postProcessBeforeInitialization(Object bean, String beanName) {
      return bean;
   }
    //初始化之前
   @Override
   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;
   }

    //销毁之前
   @Override
   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
         }
      }
   }

   @Override
   public boolean requiresDestruction(Object bean) {
      return (bean instanceof ApplicationListener);
   }

   @Override
   public boolean equals(Object other) {
      return (this == other || (other instanceof ApplicationListenerDetector &&
            this.applicationContext == ((ApplicationListenerDetector) other).applicationContext));
   }
   @Override
   public int hashCode() {
      return ObjectUtils.nullSafeHashCode(this.applicationContext);
   }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值