Skip to content

Commit

Permalink
Only start management context when parent has a web server
Browse files Browse the repository at this point in the history
Fixes gh-38554
  • Loading branch information
wilkinsona committed Nov 28, 2023
1 parent 3e4e59a commit 75a8955
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
Expand Down Expand Up @@ -82,6 +83,9 @@ private ChildManagementContextInitializer(ManagementContextFactory managementCon

@Override
public void start() {
if (!(this.parentContext instanceof WebServerApplicationContext)) {
return;
}
if (this.managementContext == null) {
ConfigurableApplicationContext managementContext = createManagementContext();
registerBeans(managementContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ void childManagementContextShouldStartForEmbeddedServer(CapturedOutput output) {
.run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2)));
}

@Test
void childManagementContextShouldNotStartWithoutEmbeddedServer(CapturedOutput output) {
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
ServletWebServerFactoryAutoConfiguration.class, ServletManagementContextAutoConfiguration.class,
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class));
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").run((context) -> {
assertThat(context).hasNotFailed();
assertThat(output).doesNotContain("Tomcat started");
});
}

@Test
void childManagementContextShouldRestartWhenParentIsStoppedThenStarted(CapturedOutput output) {
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
Expand Down

0 comments on commit 75a8955

Please sign in to comment.