概述
该BeanPostProcessor
检测那些实现了接口ApplicationListener
的bean
,在它们创建时初始化之后,将它们添加到应用上下文的事件多播器上;并在这些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);
}
}