Skip to content

Commit 1611ce7

Browse files
committed
AbstractApplicationContext silently ignores non-initialized ApplicationEventMulticaster/LifecycleProcessor on destruction
Issue: SPR-16149
1 parent d00e4f1 commit 1611ce7

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
387387
else {
388388
applicationEvent = new PayloadApplicationEvent<>(this, event);
389389
if (eventType == null) {
390-
eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType();
390+
eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
391391
}
392392
}
393393

@@ -1000,11 +1000,13 @@ protected void doClose() {
10001000
}
10011001

10021002
// Stop all Lifecycle beans, to avoid delays during individual destruction.
1003-
try {
1004-
getLifecycleProcessor().onClose();
1005-
}
1006-
catch (Throwable ex) {
1007-
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
1003+
if (this.lifecycleProcessor != null) {
1004+
try {
1005+
this.lifecycleProcessor.onClose();
1006+
}
1007+
catch (Throwable ex) {
1008+
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
1009+
}
10081010
}
10091011

10101012
// Destroy all cached singletons in the context's BeanFactory.

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,14 @@ else if (Boolean.FALSE.equals(flag)) {
9292
@Override
9393
public void postProcessBeforeDestruction(Object bean, String beanName) {
9494
if (bean instanceof ApplicationListener) {
95-
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
96-
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
97-
multicaster.removeApplicationListenerBean(beanName);
95+
try {
96+
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
97+
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
98+
multicaster.removeApplicationListenerBean(beanName);
99+
}
100+
catch (IllegalStateException ex) {
101+
// ApplicationEventMulticaster not initialized yet - no need to remove a listener
102+
}
98103
}
99104
}
100105

0 commit comments

Comments
 (0)