Browse Source

AbstractApplicationContext silently ignores non-initialized ApplicationEventMulticaster/LifecycleProcessor on destruction

Issue: SPR-16149
pull/1584/head
Juergen Hoeller 8 years ago
parent
commit
1611ce7180
  1. 14
      spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
  2. 11
      spring-context/src/main/java/org/springframework/context/support/ApplicationListenerDetector.java

14
spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

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

11
spring-context/src/main/java/org/springframework/context/support/ApplicationListenerDetector.java

@ -92,9 +92,14 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor, @@ -92,9 +92,14 @@ class ApplicationListenerDetector implements DestructionAwareBeanPostProcessor,
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) {
if (bean instanceof ApplicationListener) {
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
multicaster.removeApplicationListenerBean(beanName);
try {
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
}
}
}

Loading…
Cancel
Save