Skip to content

Commit

Permalink
Reset ApplicationEventMulticaster/MessageSource/LifecycleProcessor on…
Browse files Browse the repository at this point in the history
… close

Closes gh-21988
Closes gh-31397
  • Loading branch information
jhoeller committed Oct 10, 2023
1 parent a6c2765 commit 2754da1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,22 @@
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {

/**
* Name of the MessageSource bean in the factory.
* If none is supplied, message resolution is delegated to the parent.
* @see MessageSource
*/
public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";

/**
* Name of the LifecycleProcessor bean in the factory.
* If none is supplied, a DefaultLifecycleProcessor is used.
* @since 3.0
* @see org.springframework.context.LifecycleProcessor
* @see org.springframework.context.support.DefaultLifecycleProcessor
*/
public static final String LIFECYCLE_PROCESSOR_BEAN_NAME = "lifecycleProcessor";

/**
* Name of the MessageSource bean in the factory.
* If none is supplied, message resolution is delegated to the parent.
* @see MessageSource
*/
public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";

/**
* Name of the ApplicationEventMulticaster bean in the factory.
* If none is supplied, a default SimpleApplicationEventMulticaster is used.
Expand Down Expand Up @@ -433,8 +434,8 @@ protected void publishEvent(Object event, @Nullable ResolvableType typeHint) {
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
else if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.multicastEvent(applicationEvent, eventType);
}

// Publish event via parent context as well...
Expand Down Expand Up @@ -1093,6 +1094,11 @@ protected void doClose() {
this.applicationListeners.addAll(this.earlyApplicationListeners);
}

// Reset internal delegates.
this.applicationEventMulticaster = null;
this.messageSource = null;
this.lifecycleProcessor = null;

// Switch to inactive.
this.active.set(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,27 @@ void contextEventsAreReceived() {
assertThat(events).as("Wrong number of initial context events").hasSize(1);
assertThat(events.get(0).getClass()).isEqualTo(ContextRefreshedEvent.class);

this.context.start();
List<Object> eventsAfterStart = this.eventCollector.getEvents(listener);
assertThat(eventsAfterStart).as("Wrong number of context events on start").hasSize(2);
assertThat(eventsAfterStart.get(1).getClass()).isEqualTo(ContextStartedEvent.class);
this.eventCollector.assertTotalEventsCount(2);

this.context.stop();
List<Object> eventsAfterStop = this.eventCollector.getEvents(listener);
assertThat(eventsAfterStop).as("Wrong number of context events on shutdown").hasSize(2);
assertThat(eventsAfterStop.get(1).getClass()).isEqualTo(ContextStoppedEvent.class);
this.eventCollector.assertTotalEventsCount(2);
assertThat(eventsAfterStop).as("Wrong number of context events on stop").hasSize(3);
assertThat(eventsAfterStop.get(2).getClass()).isEqualTo(ContextStoppedEvent.class);
this.eventCollector.assertTotalEventsCount(3);

this.context.close();
List<Object> eventsAfterClose = this.eventCollector.getEvents(listener);
assertThat(eventsAfterClose).as("Wrong number of context events on close").hasSize(4);
assertThat(eventsAfterClose.get(3).getClass()).isEqualTo(ContextClosedEvent.class);
this.eventCollector.assertTotalEventsCount(4);

// Further events are supposed to be ignored after context close
this.context.publishEvent(new ContextClosedEvent(this.context));
this.eventCollector.assertTotalEventsCount(4);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
Expand Down Expand Up @@ -210,6 +211,8 @@ void defaultApplicationContextMessageSource() {
ac.refresh();
assertThat(ac.getMessage("code1", null, "default", Locale.ENGLISH)).isEqualTo("default");
assertThat(ac.getMessage("code1", new Object[]{"value"}, "default {0}", Locale.ENGLISH)).isEqualTo("default value");
ac.close();
assertThatIllegalStateException().isThrownBy(() -> ac.getMessage("code1", null, "default", Locale.ENGLISH));
}

@Test
Expand All @@ -222,6 +225,8 @@ void defaultApplicationContextMessageSourceWithParent() {
ac.refresh();
assertThat(ac.getMessage("code1", null, "default", Locale.ENGLISH)).isEqualTo("default");
assertThat(ac.getMessage("code1", new Object[]{"value"}, "default {0}", Locale.ENGLISH)).isEqualTo("default value");
ac.close();
assertThatIllegalStateException().isThrownBy(() -> ac.getMessage("code1", null, "default", Locale.ENGLISH));
}

@Test
Expand All @@ -234,6 +239,8 @@ void staticApplicationContextMessageSourceWithStaticParent() {
ac.refresh();
assertThat(ac.getMessage("code1", null, "default", Locale.ENGLISH)).isEqualTo("default");
assertThat(ac.getMessage("code1", new Object[]{"value"}, "default {0}", Locale.ENGLISH)).isEqualTo("default value");
ac.close();
assertThatIllegalStateException().isThrownBy(() -> ac.getMessage("code1", null, "default", Locale.ENGLISH));
}

@Test
Expand All @@ -246,6 +253,8 @@ void staticApplicationContextMessageSourceWithDefaultParent() {
ac.refresh();
assertThat(ac.getMessage("code1", null, "default", Locale.ENGLISH)).isEqualTo("default");
assertThat(ac.getMessage("code1", new Object[]{"value"}, "default {0}", Locale.ENGLISH)).isEqualTo("default value");
ac.close();
assertThatIllegalStateException().isThrownBy(() -> ac.getMessage("code1", null, "default", Locale.ENGLISH));
}

@Test
Expand Down

0 comments on commit 2754da1

Please sign in to comment.