Defensively handle non-retrievable ApplicationListener bean names

This commit is contained in:
Juergen Hoeller 2013-11-20 14:52:25 +01:00
parent 6a9e116d78
commit 41cdbd292a
1 changed files with 20 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.OrderComparator; import org.springframework.core.OrderComparator;
@ -160,10 +161,16 @@ public abstract class AbstractApplicationEventMulticaster implements Application
if (!listenerBeans.isEmpty()) { if (!listenerBeans.isEmpty()) {
BeanFactory beanFactory = getBeanFactory(); BeanFactory beanFactory = getBeanFactory();
for (String listenerBeanName : listenerBeans) { for (String listenerBeanName : listenerBeans) {
ApplicationListener listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class); try {
if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) { ApplicationListener listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class);
retriever.applicationListenerBeans.add(listenerBeanName); if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
allListeners.add(listener); retriever.applicationListenerBeans.add(listenerBeanName);
allListeners.add(listener);
}
}
catch (NoSuchBeanDefinitionException ex) {
// Singleton listener instance (without backing bean definition) disappeared -
// probably in the middle of the destruction phase
} }
} }
} }
@ -252,9 +259,15 @@ public abstract class AbstractApplicationEventMulticaster implements Application
if (!this.applicationListenerBeans.isEmpty()) { if (!this.applicationListenerBeans.isEmpty()) {
BeanFactory beanFactory = getBeanFactory(); BeanFactory beanFactory = getBeanFactory();
for (String listenerBeanName : this.applicationListenerBeans) { for (String listenerBeanName : this.applicationListenerBeans) {
ApplicationListener listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class); try {
if (this.preFiltered || !allListeners.contains(listener)) { ApplicationListener listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class);
allListeners.add(listener); if (this.preFiltered || !allListeners.contains(listener)) {
allListeners.add(listener);
}
}
catch (NoSuchBeanDefinitionException ex) {
// Singleton listener instance (without backing bean definition) disappeared -
// probably in the middle of the destruction phase
} }
} }
} }