提交 8ae0bd6e 编写于 作者: J Juergen Hoeller

ApplicationListenerDetector ignores non-managed bean instances

Issue: SPR-14879
上级 95abd18f
...@@ -27,6 +27,7 @@ import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcess ...@@ -27,6 +27,7 @@ import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcess
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster; import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.util.ObjectUtils;
/** /**
* {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener} * {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
...@@ -57,8 +58,8 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, ...@@ -57,8 +58,8 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
@Override @Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
if (this.applicationContext != null && beanDefinition.isSingleton()) { if (this.applicationContext != null) {
this.singletonNames.put(beanName, Boolean.TRUE); this.singletonNames.put(beanName, beanDefinition.isSingleton());
} }
} }
...@@ -76,7 +77,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, ...@@ -76,7 +77,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
// singleton bean (top-level or inner): register on the fly // singleton bean (top-level or inner): register on the fly
this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
} }
else if (flag == null) { else if (Boolean.FALSE.equals(flag)) {
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) { if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
// inner bean with other scope - can't reliably process events // inner bean with other scope - can't reliably process events
logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " + logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
...@@ -84,7 +85,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, ...@@ -84,7 +85,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
"because it does not have singleton scope. Only top-level listener beans are allowed " + "because it does not have singleton scope. Only top-level listener beans are allowed " +
"to be of non-singleton scope."); "to be of non-singleton scope.");
} }
this.singletonNames.put(beanName, Boolean.FALSE); this.singletonNames.remove(beanName);
} }
} }
return bean; return bean;
...@@ -92,7 +93,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, ...@@ -92,7 +93,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
@Override @Override
public void postProcessBeforeDestruction(Object bean, String beanName) { public void postProcessBeforeDestruction(Object bean, String beanName) {
if (bean instanceof ApplicationListener) { if (this.applicationContext != null && bean instanceof ApplicationListener) {
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster(); ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
multicaster.removeApplicationListener((ApplicationListener<?>) bean); multicaster.removeApplicationListener((ApplicationListener<?>) bean);
multicaster.removeApplicationListenerBean(beanName); multicaster.removeApplicationListenerBean(beanName);
...@@ -113,7 +114,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, ...@@ -113,7 +114,7 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
@Override @Override
public int hashCode() { public int hashCode() {
return this.applicationContext.hashCode(); return ObjectUtils.nullSafeHashCode(this.applicationContext);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册