AbstractApplicationContext silently ignores non-initialized ApplicationEventMulticaster/LifecycleProcessor on destruction

Issue: SPR-16149
This commit is contained in:
Juergen Hoeller
2017-11-02 16:07:12 +01:00
parent d00e4f17ec
commit 1611ce7180
2 changed files with 16 additions and 9 deletions

View File

@@ -387,7 +387,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
else { else {
applicationEvent = new PayloadApplicationEvent<>(this, event); applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) { if (eventType == null) {
eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType(); eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
} }
} }
@@ -1000,12 +1000,14 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
} }
// Stop all Lifecycle beans, to avoid delays during individual destruction. // Stop all Lifecycle beans, to avoid delays during individual destruction.
if (this.lifecycleProcessor != null) {
try { try {
getLifecycleProcessor().onClose(); this.lifecycleProcessor.onClose();
} }
catch (Throwable ex) { catch (Throwable ex) {
logger.warn("Exception thrown from LifecycleProcessor on context close", ex); logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
} }
}
// Destroy all cached singletons in the context's BeanFactory. // Destroy all cached singletons in the context's BeanFactory.
destroyBeans(); destroyBeans();

View File

@@ -92,10 +92,15 @@ 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 (bean instanceof ApplicationListener) {
try {
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster(); ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
multicaster.removeApplicationListener((ApplicationListener<?>) bean); multicaster.removeApplicationListener((ApplicationListener<?>) bean);
multicaster.removeApplicationListenerBean(beanName); multicaster.removeApplicationListenerBean(beanName);
} }
catch (IllegalStateException ex) {
// ApplicationEventMulticaster not initialized yet - no need to remove a listener
}
}
} }
@Override @Override