Skip to content

Commit ba330f1

Browse files
committed
AbstractApplicationContext resets local listeners to pre-refresh state
Closes gh-22325 (cherry picked from commit 0f73a69)
1 parent 4b6558c commit ba330f1

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -211,7 +211,11 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
211211
/** Statically specified listeners */
212212
private final Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<>();
213213

214-
/** ApplicationEvents published early */
214+
/** Local listeners registered before refresh */
215+
@Nullable
216+
private Set<ApplicationListener<?>> earlyApplicationListeners;
217+
218+
/** ApplicationEvents published before the multicaster setup */
215219
@Nullable
216220
private Set<ApplicationEvent> earlyApplicationEvents;
217221

@@ -485,7 +489,6 @@ public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor)
485489
this.beanFactoryPostProcessors.add(postProcessor);
486490
}
487491

488-
489492
/**
490493
* Return the list of BeanFactoryPostProcessors that will get applied
491494
* to the internal BeanFactory.
@@ -580,6 +583,7 @@ public void refresh() throws BeansException, IllegalStateException {
580583
* active flag as well as performing any initialization of property sources.
581584
*/
582585
protected void prepareRefresh() {
586+
// Switch to active.
583587
this.startupDate = System.currentTimeMillis();
584588
this.closed.set(false);
585589
this.active.set(true);
@@ -588,13 +592,23 @@ protected void prepareRefresh() {
588592
logger.info("Refreshing " + this);
589593
}
590594

591-
// Initialize any placeholder property sources in the context environment
595+
// Initialize any placeholder property sources in the context environment.
592596
initPropertySources();
593597

594-
// Validate that all properties marked as required are resolvable
598+
// Validate that all properties marked as required are resolvable:
595599
// see ConfigurablePropertyResolver#setRequiredProperties
596600
getEnvironment().validateRequiredProperties();
597601

602+
// Store pre-refresh ApplicationListeners...
603+
if (this.earlyApplicationListeners == null) {
604+
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
605+
}
606+
else {
607+
// Reset local application listeners to pre-refresh state.
608+
this.applicationListeners.clear();
609+
this.applicationListeners.addAll(this.earlyApplicationListeners);
610+
}
611+
598612
// Allow for the collection of early ApplicationEvents,
599613
// to be published once the multicaster is available...
600614
this.earlyApplicationEvents = new LinkedHashSet<>();
@@ -986,6 +1000,7 @@ public void close() {
9861000
* @see #registerShutdownHook()
9871001
*/
9881002
protected void doClose() {
1003+
// Check whether an actual close attempt is necessary...
9891004
if (this.active.get() && this.closed.compareAndSet(false, true)) {
9901005
if (logger.isInfoEnabled()) {
9911006
logger.info("Closing " + this);
@@ -1020,6 +1035,13 @@ protected void doClose() {
10201035
// Let subclasses do some final clean-up if they wish...
10211036
onClose();
10221037

1038+
// Reset local application listeners to pre-refresh state.
1039+
if (this.earlyApplicationListeners != null) {
1040+
this.applicationListeners.clear();
1041+
this.applicationListeners.addAll(this.earlyApplicationListeners);
1042+
}
1043+
1044+
// Switch to inactive.
10231045
this.active.set(false);
10241046
}
10251047
}
@@ -1294,7 +1316,7 @@ private MessageSource getMessageSource() throws IllegalStateException {
12941316
@Nullable
12951317
protected MessageSource getInternalParentMessageSource() {
12961318
return (getParent() instanceof AbstractApplicationContext ?
1297-
((AbstractApplicationContext) getParent()).messageSource : getParent());
1319+
((AbstractApplicationContext) getParent()).messageSource : getParent());
12981320
}
12991321

13001322

0 commit comments

Comments
 (0)