Skip to content

Commit 519195c

Browse files
committed
AbstractApplicationContext silently ignores non-initialized ApplicationEventMulticaster/LifecycleProcessor on destruction
Issue: SPR-16149 (cherry picked from commit 1611ce7)
1 parent 573ff2c commit 519195c

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ protected void publishEvent(Object event, ResolvableType eventType) {
381381
else {
382382
applicationEvent = new PayloadApplicationEvent<Object>(this, event);
383383
if (eventType == null) {
384-
eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType();
384+
eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
385385
}
386386
}
387387

@@ -995,11 +995,13 @@ protected void doClose() {
995995
}
996996

997997
// Stop all Lifecycle beans, to avoid delays during individual destruction.
998-
try {
999-
getLifecycleProcessor().onClose();
1000-
}
1001-
catch (Throwable ex) {
1002-
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
998+
if (this.lifecycleProcessor != null) {
999+
try {
1000+
this.lifecycleProcessor.onClose();
1001+
}
1002+
catch (Throwable ex) {
1003+
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
1004+
}
10031005
}
10041006

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

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,9 +94,14 @@ else if (Boolean.FALSE.equals(flag)) {
9494
@Override
9595
public void postProcessBeforeDestruction(Object bean, String beanName) {
9696
if (this.applicationContext != null && bean instanceof ApplicationListener) {
97-
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
98-
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
99-
multicaster.removeApplicationListenerBean(beanName);
97+
try {
98+
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
99+
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
100+
multicaster.removeApplicationListenerBean(beanName);
101+
}
102+
catch (IllegalStateException ex) {
103+
// ApplicationEventMulticaster not initialized yet - no need to remove a listener
104+
}
100105
}
101106
}
102107

0 commit comments

Comments
 (0)