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.
@@ -211,7 +211,11 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
211
211
/** Statically specified listeners */
212
212
private final Set <ApplicationListener <?>> applicationListeners = new LinkedHashSet <>();
213
213
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 */
215
219
@ Nullable
216
220
private Set <ApplicationEvent > earlyApplicationEvents ;
217
221
@@ -485,7 +489,6 @@ public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor)
485
489
this .beanFactoryPostProcessors .add (postProcessor );
486
490
}
487
491
488
-
489
492
/**
490
493
* Return the list of BeanFactoryPostProcessors that will get applied
491
494
* to the internal BeanFactory.
@@ -580,6 +583,7 @@ public void refresh() throws BeansException, IllegalStateException {
580
583
* active flag as well as performing any initialization of property sources.
581
584
*/
582
585
protected void prepareRefresh () {
586
+ // Switch to active.
583
587
this .startupDate = System .currentTimeMillis ();
584
588
this .closed .set (false );
585
589
this .active .set (true );
@@ -588,13 +592,23 @@ protected void prepareRefresh() {
588
592
logger .info ("Refreshing " + this );
589
593
}
590
594
591
- // Initialize any placeholder property sources in the context environment
595
+ // Initialize any placeholder property sources in the context environment.
592
596
initPropertySources ();
593
597
594
- // Validate that all properties marked as required are resolvable
598
+ // Validate that all properties marked as required are resolvable:
595
599
// see ConfigurablePropertyResolver#setRequiredProperties
596
600
getEnvironment ().validateRequiredProperties ();
597
601
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
+
598
612
// Allow for the collection of early ApplicationEvents,
599
613
// to be published once the multicaster is available...
600
614
this .earlyApplicationEvents = new LinkedHashSet <>();
@@ -986,6 +1000,7 @@ public void close() {
986
1000
* @see #registerShutdownHook()
987
1001
*/
988
1002
protected void doClose () {
1003
+ // Check whether an actual close attempt is necessary...
989
1004
if (this .active .get () && this .closed .compareAndSet (false , true )) {
990
1005
if (logger .isInfoEnabled ()) {
991
1006
logger .info ("Closing " + this );
@@ -1020,6 +1035,13 @@ protected void doClose() {
1020
1035
// Let subclasses do some final clean-up if they wish...
1021
1036
onClose ();
1022
1037
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.
1023
1045
this .active .set (false );
1024
1046
}
1025
1047
}
@@ -1294,7 +1316,7 @@ private MessageSource getMessageSource() throws IllegalStateException {
1294
1316
@ Nullable
1295
1317
protected MessageSource getInternalParentMessageSource () {
1296
1318
return (getParent () instanceof AbstractApplicationContext ?
1297
- ((AbstractApplicationContext ) getParent ()).messageSource : getParent ());
1319
+ ((AbstractApplicationContext ) getParent ()).messageSource : getParent ());
1298
1320
}
1299
1321
1300
1322
0 commit comments