From 9a1ca2fe83734fbce5fb3734dfc9b4a48284f536 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 10 Jun 2025 10:31:37 +0100 Subject: [PATCH] Stop ignoring prefixed environment variables in management context Binding in the child context does not work correctly when an environment prefix has been configured. The prefix is not applied to the child context's Environment and, therefore, prefixed environment variables are ignored during binding. We can fix the problem by reusing the parent context's ManagementServerProperties rather than binding them again in the child context. Doing so will fix the problem reported in gh-45857 that was introduced in 020fd7b and will also avoid an unnecessary second binding of the properties. gh-45858 may fix the problem more generally by applying the prefix to the child context's environment. This would benefit situations where the properties need to be bound in the child context because they haven't already been bound in the parent. Closes gh-45847 --- ...etManagementChildContextConfiguration.java | 2 -- ...agementChildContextConfigurationTests.java | 22 +++++++++++++++++++ ...agementChildContextConfigurationTests.java | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java index fefe79cb1a..9f439939fe 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java @@ -39,7 +39,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.web.ServerProperties; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; @@ -67,7 +66,6 @@ import org.springframework.util.StringUtils; */ @ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false) @ConditionalOnWebApplication(type = Type.SERVLET) -@EnableConfigurationProperties(ManagementServerProperties.class) class ServletManagementChildContextConfiguration { @Bean diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java index 7bfbd7153a..10cdda011b 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java @@ -19,6 +19,8 @@ package org.springframework.boot.actuate.autoconfigure.web.reactive; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration.AccessLogCustomizer; +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties; +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import static org.assertj.core.api.Assertions.assertThat; @@ -46,4 +48,24 @@ class ReactiveManagementChildContextConfigurationTests { assertThat(customizer.customizePrefix("existing")).isEqualTo("existing"); } + @Test + // gh-45857 + void failsWithoutManagementServerPropertiesBeanFromParent() { + new ReactiveWebApplicationContextRunner().run((parent) -> { + new ReactiveWebApplicationContextRunner().withParent(parent) + .withUserConfiguration(ReactiveManagementChildContextConfiguration.class) + .run((context) -> assertThat(context).hasFailed()); + }); + } + + @Test + // gh-45857 + void succeedsWithManagementServerPropertiesBeanFromParent() { + new ReactiveWebApplicationContextRunner().withBean(ManagementServerProperties.class).run((parent) -> { + new ReactiveWebApplicationContextRunner().withParent(parent) + .withUserConfiguration(ReactiveManagementChildContextConfiguration.class) + .run((context) -> assertThat(context).hasNotFailed()); + }); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java index a46dca62ca..e4c3e3d051 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java @@ -18,7 +18,9 @@ package org.springframework.boot.actuate.autoconfigure.web.servlet; import org.junit.jupiter.api.Test; +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties; import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration.AccessLogCustomizer; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import static org.assertj.core.api.Assertions.assertThat; @@ -46,4 +48,24 @@ class ServletManagementChildContextConfigurationTests { assertThat(customizer.customizePrefix("existing")).isEqualTo("existing"); } + @Test + // gh-45857 + void failsWithoutManagementServerPropertiesBeanFromParent() { + new WebApplicationContextRunner().run((parent) -> { + new WebApplicationContextRunner().withParent(parent) + .withUserConfiguration(ServletManagementChildContextConfiguration.class) + .run((context) -> assertThat(context).hasFailed()); + }); + } + + @Test + // gh-45857 + void succeedsWithManagementServerPropertiesBeanFromParent() { + new WebApplicationContextRunner().withBean(ManagementServerProperties.class).run((parent) -> { + new WebApplicationContextRunner().withParent(parent) + .withUserConfiguration(ServletManagementChildContextConfiguration.class) + .run((context) -> assertThat(context).hasNotFailed()); + }); + } + }