Skip to content

Commit 75a8955

Browse files
committed
Only start management context when parent has a web server
Fixes spring-projectsgh-38554
1 parent 3e4e59a commit 75a8955

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3434
import org.springframework.boot.context.event.ApplicationFailedEvent;
3535
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
36+
import org.springframework.boot.web.context.WebServerApplicationContext;
3637
import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle;
3738
import org.springframework.context.ApplicationContext;
3839
import org.springframework.context.ApplicationContextInitializer;
@@ -82,6 +83,9 @@ private ChildManagementContextInitializer(ManagementContextFactory managementCon
8283

8384
@Override
8485
public void start() {
86+
if (!(this.parentContext instanceof WebServerApplicationContext)) {
87+
return;
88+
}
8589
if (this.managementContext == null) {
8690
ConfigurableApplicationContext managementContext = createManagementContext();
8791
registerBeans(managementContext);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ void childManagementContextShouldStartForEmbeddedServer(CapturedOutput output) {
5555
.run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2)));
5656
}
5757

58+
@Test
59+
void childManagementContextShouldNotStartWithoutEmbeddedServer(CapturedOutput output) {
60+
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
61+
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
62+
ServletWebServerFactoryAutoConfiguration.class, ServletManagementContextAutoConfiguration.class,
63+
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class));
64+
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").run((context) -> {
65+
assertThat(context).hasNotFailed();
66+
assertThat(output).doesNotContain("Tomcat started");
67+
});
68+
}
69+
5870
@Test
5971
void childManagementContextShouldRestartWhenParentIsStoppedThenStarted(CapturedOutput output) {
6072
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(

0 commit comments

Comments
 (0)