Merge branch '3.1.x'

Closes gh-37402
pull/37434/head
Andy Wilkinson 1 year ago
commit 7424e845d1

@ -296,6 +296,9 @@ public class SpringApplication {
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
if (this.registerShutdownHook) {
SpringApplication.shutdownHook.enableShutdowHookAddition();
}
long startTime = System.nanoTime();
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;

@ -62,12 +62,18 @@ class SpringApplicationShutdownHook implements Runnable {
private final AtomicBoolean shutdownHookAdded = new AtomicBoolean();
private volatile boolean shutdownHookAdditionEnabled = false;
private boolean inProgress;
SpringApplicationShutdownHandlers getHandlers() {
return this.handlers;
}
void enableShutdowHookAddition() {
this.shutdownHookAdditionEnabled = true;
}
void registerApplicationContext(ConfigurableApplicationContext context) {
addRuntimeShutdownHookIfNecessary();
synchronized (SpringApplicationShutdownHook.class) {
@ -78,7 +84,7 @@ class SpringApplicationShutdownHook implements Runnable {
}
private void addRuntimeShutdownHookIfNecessary() {
if (this.shutdownHookAdded.compareAndSet(false, true)) {
if (this.shutdownHookAdditionEnabled && this.shutdownHookAdded.compareAndSet(false, true)) {
addRuntimeShutdownHook();
}
}

@ -51,6 +51,7 @@ class SpringApplicationShutdownHookTests {
@Test
void shutdownHookIsNotAddedUntilContextIsRegistered() {
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
shutdownHook.enableShutdowHookAddition();
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
ConfigurableApplicationContext context = new GenericApplicationContext();
shutdownHook.registerApplicationContext(context);
@ -60,12 +61,25 @@ class SpringApplicationShutdownHookTests {
@Test
void shutdownHookIsNotAddedUntilHandlerIsRegistered() {
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
shutdownHook.enableShutdowHookAddition();
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
shutdownHook.getHandlers().add(() -> {
});
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
}
@Test
void shutdownHookIsNotAddedUntilAdditionIsEnabled() {
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
shutdownHook.getHandlers().add(() -> {
});
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
shutdownHook.enableShutdowHookAddition();
shutdownHook.getHandlers().add(() -> {
});
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
}
@Test
void runClosesContextsBeforeRunningHandlerActions() {
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();

Loading…
Cancel
Save