Spring BeanPostProcessor : ApplicationListenerDetector

概述

BeanPostProcessor检测那些实现了接口ApplicationListenerbean,在它们创建时初始化之后,将它们添加到应用上下文的事件多播器上;并在这些ApplicationListener bean销毁之前,将它们从应用上下文的事件多播器上移除。

源代码解析

package org.springframework.context.support;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.util.ObjectUtils;

/**
 * @author Juergen Hoeller
 * @since 4.3.4
 */
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;
	}

	// BeanPostProcessor 定义的方法,在每个bean创建时初始化之后应用
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		if (bean instanceof ApplicationListener) {
			// 检测到bean是一个 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
				// 如果当前 ApplicationListener bean scope 是 singleton 单例模式,
				// 则将它注册到应用的事件多播器上
				this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
			}
			else if (Boolean.FALSE.equals(flag)) {
				// 如果当前 ApplicationListener bean scope 不是 singleton 单例模式,
				// 则尝试输出警告日志,说明情况
				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;
	}

	// DestructionAwareBeanPostProcessor 接口定义的方法,
	// 在 bean 销毁前被调用
	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) {
		if (bean instanceof ApplicationListener) {
		// 如果当前 bean 是一个 ApplicationListener 才执行这段逻辑
			try {
				// 当前  bean 是一个 ApplicationListener, 现在该bean即将销毁,
				// 因此从应用上下文的事件多播器上将其移除
				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) {
		// DestructionAwareBeanPostProcessor 接口定义的方法,
		// 用于检测对于某个 bean 是否要调用 postProcessBeforeDestruction 方法
		// 该实现表明仅在 bean 是一个 ApplicationListener 时才调用上面的
		// postProcessBeforeDestruction 
		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、付费专栏及课程。

余额充值