Only start management context when parent has a web server

Fixes gh-38554
This commit is contained in:
Andy Wilkinson 2023-11-27 12:18:15 +00:00 committed by Andy Wilkinson
parent 3e4e59a8f0
commit 75a8955659
2 changed files with 16 additions and 0 deletions

View File

@ -33,6 +33,7 @@ import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFacto
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;
@ -82,6 +83,9 @@ class ChildManagementContextInitializer implements BeanRegistrationAotProcessor,
@Override
public void start() {
if (!(this.parentContext instanceof WebServerApplicationContext)) {
return;
}
if (this.managementContext == null) {
ConfigurableApplicationContext managementContext = createManagementContext();
registerBeans(managementContext);

View File

@ -55,6 +55,18 @@ class ManagementContextAutoConfigurationTests {
.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(