Browse Source

Merge branch '3.0.x' into 3.1.x

Closes gh-37920
pull/38706/head
Andy Wilkinson 2 years ago
parent
commit
60a0b7eedb
  1. 16
      spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java
  2. 4
      spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java
  3. 37
      spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java
  4. 11
      spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java

16
spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import org.springframework.boot.context.event.ApplicationFailedEvent; @@ -23,6 +23,7 @@ import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.devtools.system.DevToolsEnablementDeducer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
@ -66,7 +67,14 @@ public class RestartApplicationListener implements ApplicationListener<Applicati @@ -66,7 +67,14 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
String enabled = System.getProperty(ENABLED_PROPERTY);
RestartInitializer restartInitializer = null;
if (enabled == null) {
restartInitializer = new DefaultRestartInitializer();
if (implicitlyEnableRestart()) {
restartInitializer = new DefaultRestartInitializer();
}
else {
logger.info("Restart disabled due to context in which it is running");
Restarter.disable();
return;
}
}
else if (Boolean.parseBoolean(enabled)) {
restartInitializer = new DefaultRestartInitializer() {
@ -96,6 +104,10 @@ public class RestartApplicationListener implements ApplicationListener<Applicati @@ -96,6 +104,10 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
}
}
boolean implicitlyEnableRestart() {
return DevToolsEnablementDeducer.shouldEnable(Thread.currentThread());
}
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
Restarter.getInstance().prepare(event.getApplicationContext());
}

4
spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -408,7 +408,7 @@ public class Restarter { @@ -408,7 +408,7 @@ public class Restarter {
}
void prepare(ConfigurableApplicationContext applicationContext) {
if (applicationContext != null && applicationContext.getParent() != null) {
if (!this.enabled || (applicationContext != null && applicationContext.getParent() != null)) {
return;
}
if (applicationContext instanceof GenericApplicationContext genericContext) {

37
spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -66,7 +66,7 @@ class RestartApplicationListenerTests { @@ -66,7 +66,7 @@ class RestartApplicationListenerTests {
@Test
void initializeWithReady() {
testInitialize(false);
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("args", ARGS);
assertThat(Restarter.getInstance().isFinished()).isTrue();
assertThat((List<?>) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isNotEmpty();
@ -74,7 +74,7 @@ class RestartApplicationListenerTests { @@ -74,7 +74,7 @@ class RestartApplicationListenerTests {
@Test
void initializeWithFail() {
testInitialize(true);
testInitialize(true, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("args", ARGS);
assertThat(Restarter.getInstance().isFinished()).isTrue();
assertThat((List<?>) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isEmpty();
@ -83,7 +83,7 @@ class RestartApplicationListenerTests { @@ -83,7 +83,7 @@ class RestartApplicationListenerTests {
@Test
void disableWithSystemProperty(CapturedOutput output) {
System.setProperty(ENABLED_PROPERTY, "false");
testInitialize(false);
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false);
assertThat(output).contains("Restart disabled due to System property");
}
@ -91,14 +91,28 @@ class RestartApplicationListenerTests { @@ -91,14 +91,28 @@ class RestartApplicationListenerTests {
@Test
void enableWithSystemProperty(CapturedOutput output) {
System.setProperty(ENABLED_PROPERTY, "true");
testInitialize(false);
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", true);
assertThat(output).contains("Restart enabled irrespective of application packaging due to System property");
}
private void testInitialize(boolean failed) {
@Test
void enableWithSystemPropertyWhenImplicitlyDisabled(CapturedOutput output) {
System.setProperty(ENABLED_PROPERTY, "true");
testInitialize(false, new RestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", true);
assertThat(output).contains("Restart enabled irrespective of application packaging due to System property");
}
@Test
void implicitlyDisabledInTests(CapturedOutput output) {
testInitialize(false, new RestartApplicationListener());
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false);
assertThat(output).contains("Restart disabled due to context in which it is running");
}
private void testInitialize(boolean failed, RestartApplicationListener listener) {
Restarter.clearInstance();
RestartApplicationListener listener = new RestartApplicationListener();
DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
SpringApplication application = new SpringApplication();
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
@ -114,4 +128,13 @@ class RestartApplicationListenerTests { @@ -114,4 +128,13 @@ class RestartApplicationListenerTests {
}
}
private static class ImplicitlyEnabledRestartApplicationListener extends RestartApplicationListener {
@Override
boolean implicitlyEnableRestart() {
return true;
}
}
}

11
spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java

@ -24,6 +24,7 @@ import java.util.Collections; @@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -37,6 +38,7 @@ import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles; @@ -37,6 +38,7 @@ import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.annotation.EnableScheduling;
@ -44,6 +46,7 @@ import org.springframework.scheduling.annotation.Scheduled; @@ -44,6 +46,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@ -90,6 +93,14 @@ class RestarterTests { @@ -90,6 +93,14 @@ class RestarterTests {
});
}
@Test
void testDisabled() {
Restarter.disable();
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
Restarter.getInstance().prepare(context);
assertThat(Restarter.getInstance()).extracting("rootContexts", as(InstanceOfAssertFactories.LIST)).isEmpty();
}
@Test
@SuppressWarnings("rawtypes")
void getOrAddAttributeWithNewAttribute() {

Loading…
Cancel
Save