mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '3.1.x'
Closes gh-37402
This commit is contained in:
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…
Reference in New Issue
Block a user