Skip to content

Commit 9a1ca2f

Browse files
committed
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
1 parent f66159e commit 9a1ca2f

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
4040
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
4141
import org.springframework.boot.autoconfigure.web.ServerProperties;
42-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4342
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
4443
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
4544
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
@@ -67,7 +66,6 @@
6766
*/
6867
@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false)
6968
@ConditionalOnWebApplication(type = Type.SERVLET)
70-
@EnableConfigurationProperties(ManagementServerProperties.class)
7169
class ServletManagementChildContextConfiguration {
7270

7371
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfigurationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration.AccessLogCustomizer;
22+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
23+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
2224

2325
import static org.assertj.core.api.Assertions.assertThat;
2426

@@ -46,4 +48,24 @@ void accessLogCustomizerWithNullPrefix() {
4648
assertThat(customizer.customizePrefix("existing")).isEqualTo("existing");
4749
}
4850

51+
@Test
52+
// gh-45857
53+
void failsWithoutManagementServerPropertiesBeanFromParent() {
54+
new ReactiveWebApplicationContextRunner().run((parent) -> {
55+
new ReactiveWebApplicationContextRunner().withParent(parent)
56+
.withUserConfiguration(ReactiveManagementChildContextConfiguration.class)
57+
.run((context) -> assertThat(context).hasFailed());
58+
});
59+
}
60+
61+
@Test
62+
// gh-45857
63+
void succeedsWithManagementServerPropertiesBeanFromParent() {
64+
new ReactiveWebApplicationContextRunner().withBean(ManagementServerProperties.class).run((parent) -> {
65+
new ReactiveWebApplicationContextRunner().withParent(parent)
66+
.withUserConfiguration(ReactiveManagementChildContextConfiguration.class)
67+
.run((context) -> assertThat(context).hasNotFailed());
68+
});
69+
}
70+
4971
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfigurationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2122
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration.AccessLogCustomizer;
23+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2224

2325
import static org.assertj.core.api.Assertions.assertThat;
2426

@@ -46,4 +48,24 @@ void accessLogCustomizerWithNullPrefix() {
4648
assertThat(customizer.customizePrefix("existing")).isEqualTo("existing");
4749
}
4850

51+
@Test
52+
// gh-45857
53+
void failsWithoutManagementServerPropertiesBeanFromParent() {
54+
new WebApplicationContextRunner().run((parent) -> {
55+
new WebApplicationContextRunner().withParent(parent)
56+
.withUserConfiguration(ServletManagementChildContextConfiguration.class)
57+
.run((context) -> assertThat(context).hasFailed());
58+
});
59+
}
60+
61+
@Test
62+
// gh-45857
63+
void succeedsWithManagementServerPropertiesBeanFromParent() {
64+
new WebApplicationContextRunner().withBean(ManagementServerProperties.class).run((parent) -> {
65+
new WebApplicationContextRunner().withParent(parent)
66+
.withUserConfiguration(ServletManagementChildContextConfiguration.class)
67+
.run((context) -> assertThat(context).hasNotFailed());
68+
});
69+
}
70+
4971
}

0 commit comments

Comments
 (0)