Skip to content

Commit 85c186a

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-37919
2 parents f645ef5 + 1f0e311 commit 85c186a

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

Diff for: spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.context.event.ApplicationPreparedEvent;
2424
import org.springframework.boot.context.event.ApplicationReadyEvent;
2525
import org.springframework.boot.context.event.ApplicationStartingEvent;
26+
import org.springframework.boot.devtools.system.DevToolsEnablementDeducer;
2627
import org.springframework.context.ApplicationEvent;
2728
import org.springframework.context.ApplicationListener;
2829
import org.springframework.core.Ordered;
@@ -66,7 +67,14 @@ private void onApplicationStartingEvent(ApplicationStartingEvent event) {
6667
String enabled = System.getProperty(ENABLED_PROPERTY);
6768
RestartInitializer restartInitializer = null;
6869
if (enabled == null) {
69-
restartInitializer = new DefaultRestartInitializer();
70+
if (implicitlyEnableRestart()) {
71+
restartInitializer = new DefaultRestartInitializer();
72+
}
73+
else {
74+
logger.info("Restart disabled due to context in which it is running");
75+
Restarter.disable();
76+
return;
77+
}
7078
}
7179
else if (Boolean.parseBoolean(enabled)) {
7280
restartInitializer = new DefaultRestartInitializer() {
@@ -96,6 +104,10 @@ protected boolean isDevelopmentClassLoader(ClassLoader classLoader) {
96104
}
97105
}
98106

107+
boolean implicitlyEnableRestart() {
108+
return DevToolsEnablementDeducer.shouldEnable(Thread.currentThread());
109+
}
110+
99111
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
100112
Restarter.getInstance().prepare(event.getApplicationContext());
101113
}

Diff for: spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/Restarter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -408,7 +408,7 @@ boolean isFinished() {
408408
}
409409

410410
void prepare(ConfigurableApplicationContext applicationContext) {
411-
if (applicationContext != null && applicationContext.getParent() != null) {
411+
if (!this.enabled || (applicationContext != null && applicationContext.getParent() != null)) {
412412
return;
413413
}
414414
if (applicationContext instanceof GenericApplicationContext genericContext) {

Diff for: spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,15 +66,15 @@ void isHighestPriority() {
6666

6767
@Test
6868
void initializeWithReady() {
69-
testInitialize(false);
69+
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
7070
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("args", ARGS);
7171
assertThat(Restarter.getInstance().isFinished()).isTrue();
7272
assertThat((List<?>) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isNotEmpty();
7373
}
7474

7575
@Test
7676
void initializeWithFail() {
77-
testInitialize(true);
77+
testInitialize(true, new ImplicitlyEnabledRestartApplicationListener());
7878
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("args", ARGS);
7979
assertThat(Restarter.getInstance().isFinished()).isTrue();
8080
assertThat((List<?>) ReflectionTestUtils.getField(Restarter.getInstance(), "rootContexts")).isEmpty();
@@ -83,22 +83,36 @@ void initializeWithFail() {
8383
@Test
8484
void disableWithSystemProperty(CapturedOutput output) {
8585
System.setProperty(ENABLED_PROPERTY, "false");
86-
testInitialize(false);
86+
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
8787
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false);
8888
assertThat(output).contains("Restart disabled due to System property");
8989
}
9090

9191
@Test
9292
void enableWithSystemProperty(CapturedOutput output) {
9393
System.setProperty(ENABLED_PROPERTY, "true");
94-
testInitialize(false);
94+
testInitialize(false, new ImplicitlyEnabledRestartApplicationListener());
9595
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", true);
9696
assertThat(output).contains("Restart enabled irrespective of application packaging due to System property");
9797
}
9898

99-
private void testInitialize(boolean failed) {
99+
@Test
100+
void enableWithSystemPropertyWhenImplicitlyDisabled(CapturedOutput output) {
101+
System.setProperty(ENABLED_PROPERTY, "true");
102+
testInitialize(false, new RestartApplicationListener());
103+
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", true);
104+
assertThat(output).contains("Restart enabled irrespective of application packaging due to System property");
105+
}
106+
107+
@Test
108+
void implicitlyDisabledInTests(CapturedOutput output) {
109+
testInitialize(false, new RestartApplicationListener());
110+
assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false);
111+
assertThat(output).contains("Restart disabled due to context in which it is running");
112+
}
113+
114+
private void testInitialize(boolean failed, RestartApplicationListener listener) {
100115
Restarter.clearInstance();
101-
RestartApplicationListener listener = new RestartApplicationListener();
102116
DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
103117
SpringApplication application = new SpringApplication();
104118
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
@@ -114,4 +128,13 @@ private void testInitialize(boolean failed) {
114128
}
115129
}
116130

131+
private static class ImplicitlyEnabledRestartApplicationListener extends RestartApplicationListener {
132+
133+
@Override
134+
boolean implicitlyEnableRestart() {
135+
return true;
136+
}
137+
138+
}
139+
117140
}

Diff for: spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.ThreadFactory;
2525
import java.util.concurrent.atomic.AtomicBoolean;
2626

27+
import org.assertj.core.api.InstanceOfAssertFactories;
2728
import org.awaitility.Awaitility;
2829
import org.junit.jupiter.api.AfterEach;
2930
import org.junit.jupiter.api.BeforeEach;
@@ -37,13 +38,15 @@
3738
import org.springframework.boot.test.system.CapturedOutput;
3839
import org.springframework.boot.test.system.OutputCaptureExtension;
3940
import org.springframework.context.ApplicationListener;
41+
import org.springframework.context.ConfigurableApplicationContext;
4042
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4143
import org.springframework.context.event.ContextClosedEvent;
4244
import org.springframework.scheduling.annotation.EnableScheduling;
4345
import org.springframework.scheduling.annotation.Scheduled;
4446
import org.springframework.stereotype.Component;
4547
import org.springframework.util.StringUtils;
4648

49+
import static org.assertj.core.api.Assertions.as;
4750
import static org.assertj.core.api.Assertions.assertThat;
4851
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4952
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -90,6 +93,14 @@ void testRestart(CapturedOutput output) {
9093
});
9194
}
9295

96+
@Test
97+
void testDisabled() {
98+
Restarter.disable();
99+
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
100+
Restarter.getInstance().prepare(context);
101+
assertThat(Restarter.getInstance()).extracting("rootContexts", as(InstanceOfAssertFactories.LIST)).isEmpty();
102+
}
103+
93104
@Test
94105
@SuppressWarnings("rawtypes")
95106
void getOrAddAttributeWithNewAttribute() {

0 commit comments

Comments
 (0)