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
This commit is contained in:
Andy Wilkinson
2025-06-10 10:31:37 +01:00
parent f66159e4b1
commit 9a1ca2fe83
3 changed files with 44 additions and 2 deletions

View File

@@ -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

View File

@@ -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());
});
}
}

View File

@@ -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());
});
}
}