1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -212,7 +212,11 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
212
212
/** Statically specified listeners. */
213
213
private final Set <ApplicationListener <?>> applicationListeners = new LinkedHashSet <>();
214
214
215
- /** ApplicationEvents published early. */
215
+ /** Local listeners registered before refresh. */
216
+ @ Nullable
217
+ private Set <ApplicationListener <?>> earlyApplicationListeners ;
218
+
219
+ /** ApplicationEvents published before the multicaster setup. */
216
220
@ Nullable
217
221
private Set <ApplicationEvent > earlyApplicationEvents ;
218
222
@@ -483,7 +487,6 @@ public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor)
483
487
this .beanFactoryPostProcessors .add (postProcessor );
484
488
}
485
489
486
-
487
490
/**
488
491
* Return the list of BeanFactoryPostProcessors that will get applied
489
492
* to the internal BeanFactory.
@@ -578,6 +581,7 @@ public void refresh() throws BeansException, IllegalStateException {
578
581
* active flag as well as performing any initialization of property sources.
579
582
*/
580
583
protected void prepareRefresh () {
584
+ // Switch to active.
581
585
this .startupDate = System .currentTimeMillis ();
582
586
this .closed .set (false );
583
587
this .active .set (true );
@@ -591,13 +595,23 @@ protected void prepareRefresh() {
591
595
}
592
596
}
593
597
594
- // Initialize any placeholder property sources in the context environment
598
+ // Initialize any placeholder property sources in the context environment.
595
599
initPropertySources ();
596
600
597
- // Validate that all properties marked as required are resolvable
601
+ // Validate that all properties marked as required are resolvable:
598
602
// see ConfigurablePropertyResolver#setRequiredProperties
599
603
getEnvironment ().validateRequiredProperties ();
600
604
605
+ // Store pre-refresh ApplicationListeners...
606
+ if (this .earlyApplicationListeners == null ) {
607
+ this .earlyApplicationListeners = new LinkedHashSet <>(this .applicationListeners );
608
+ }
609
+ else {
610
+ // Reset local application listeners to pre-refresh state.
611
+ this .applicationListeners .clear ();
612
+ this .applicationListeners .addAll (this .earlyApplicationListeners );
613
+ }
614
+
601
615
// Allow for the collection of early ApplicationEvents,
602
616
// to be published once the multicaster is available...
603
617
this .earlyApplicationEvents = new LinkedHashSet <>();
@@ -982,6 +996,7 @@ public void close() {
982
996
* @see #registerShutdownHook()
983
997
*/
984
998
protected void doClose () {
999
+ // Check whether an actual close attempt is necessary...
985
1000
if (this .active .get () && this .closed .compareAndSet (false , true )) {
986
1001
if (logger .isDebugEnabled ()) {
987
1002
logger .debug ("Closing " + this );
@@ -1016,6 +1031,13 @@ protected void doClose() {
1016
1031
// Let subclasses do some final clean-up if they wish...
1017
1032
onClose ();
1018
1033
1034
+ // Reset local application listeners to pre-refresh state.
1035
+ if (this .earlyApplicationListeners != null ) {
1036
+ this .applicationListeners .clear ();
1037
+ this .applicationListeners .addAll (this .earlyApplicationListeners );
1038
+ }
1039
+
1040
+ // Switch to inactive.
1019
1041
this .active .set (false );
1020
1042
}
1021
1043
}
@@ -1302,7 +1324,7 @@ private MessageSource getMessageSource() throws IllegalStateException {
1302
1324
@ Nullable
1303
1325
protected MessageSource getInternalParentMessageSource () {
1304
1326
return (getParent () instanceof AbstractApplicationContext ?
1305
- ((AbstractApplicationContext ) getParent ()).messageSource : getParent ());
1327
+ ((AbstractApplicationContext ) getParent ()).messageSource : getParent ());
1306
1328
}
1307
1329
1308
1330
0 commit comments