From 20a225c8848a37bf890e44dda68bed43fe95b267 Mon Sep 17 00:00:00 2001 From: yongjunhong Date: Mon, 18 Aug 2025 23:30:02 +0900 Subject: [PATCH 1/3] Fix missing environment prefix in child management context Signed-off-by: yongjunhong --- .../web/server/ManagementContextFactory.java | 13 +++- .../server/ManagementContextFactoryTest.java | 60 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java diff --git a/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java b/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java index 7345532d606e..a382adee3161 100644 --- a/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java +++ b/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java @@ -30,6 +30,7 @@ import org.springframework.context.annotation.AnnotationConfigRegistry; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; import org.springframework.util.Assert; /** @@ -40,6 +41,7 @@ * * @author Andy Wilkinson * @author Phillip Webb + * @author Yongjun Hong * @since 4.0.0 */ public final class ManagementContextFactory { @@ -63,7 +65,11 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext .createEnvironment(this.webApplicationType); Assert.state(childEnvironment != null, "'childEnvironment' must not be null"); if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) { - childEnvironment.setConversionService((configurableEnvironment).getConversionService()); + configurableEnvironment.getPropertySources().forEach(propertySource -> { + if (isManagementPropertySource(propertySource, childEnvironment)) { + childEnvironment.getPropertySources().addLast(propertySource); + } + }); } ConfigurableApplicationContext managementContext = ApplicationContextFactory.DEFAULT .create(this.webApplicationType); @@ -73,6 +79,11 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext return managementContext; } + private boolean isManagementPropertySource(PropertySource propertySource, ConfigurableEnvironment childEnvironment) { + return propertySource.getName().contains("management") + && !childEnvironment.getPropertySources().contains(propertySource.getName()); + } + public void registerWebServerFactoryBeans(ApplicationContext parentContext, ConfigurableApplicationContext managementContext, AnnotationConfigRegistry registry) { if (this.autoConfigurationClasses != null && this.autoConfigurationClasses.length > 0) { diff --git a/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java new file mode 100644 index 000000000000..9c03ed9e43d6 --- /dev/null +++ b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.web.server; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.WebApplicationType; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Test for {@link ManagementContextFactory}. + * + * author Yongjun Hong + */ +class ManagementContextFactoryTest { + + @Test + void createManagementContextCopiesManagementPropertySources() { + ApplicationContext parentContext = mock(ApplicationContext.class); + ConfigurableEnvironment parentEnvironment = mock(ConfigurableEnvironment.class); + MutablePropertySources parentPropertySources = new MutablePropertySources(); + PropertySource managementPropertySource = new PropertySource<>("managementProperty") { + @Override + public Object getProperty(String name) { + return null; + } + }; + parentPropertySources.addLast(managementPropertySource); + when(parentEnvironment.getPropertySources()).thenReturn(parentPropertySources); + when(parentContext.getEnvironment()).thenReturn(parentEnvironment); + + ManagementContextFactory factory = new ManagementContextFactory(WebApplicationType.SERVLET, null); + + ConfigurableApplicationContext managementContext = factory.createManagementContext(parentContext); + + ConfigurableEnvironment childEnvironment = managementContext.getEnvironment(); + assertThat(childEnvironment.getPropertySources().contains("managementProperty")).isTrue(); + } +} \ No newline at end of file From 2a74d15b39fdffe6ca6085a4ba7b75bff9cceb25 Mon Sep 17 00:00:00 2001 From: yongjunhong Date: Mon, 18 Aug 2025 23:42:28 +0900 Subject: [PATCH 2/3] run format Signed-off-by: yongjunhong --- .../web/server/ManagementContextFactory.java | 5 +++-- ...ryTest.java => ManagementContextFactoryTests.java} | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) rename module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/{ManagementContextFactoryTest.java => ManagementContextFactoryTests.java} (88%) diff --git a/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java b/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java index a382adee3161..38c724ebf92b 100644 --- a/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java +++ b/module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java @@ -65,7 +65,7 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext .createEnvironment(this.webApplicationType); Assert.state(childEnvironment != null, "'childEnvironment' must not be null"); if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) { - configurableEnvironment.getPropertySources().forEach(propertySource -> { + configurableEnvironment.getPropertySources().forEach((propertySource) -> { if (isManagementPropertySource(propertySource, childEnvironment)) { childEnvironment.getPropertySources().addLast(propertySource); } @@ -79,7 +79,8 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext return managementContext; } - private boolean isManagementPropertySource(PropertySource propertySource, ConfigurableEnvironment childEnvironment) { + private boolean isManagementPropertySource(PropertySource propertySource, + ConfigurableEnvironment childEnvironment) { return propertySource.getName().contains("management") && !childEnvironment.getPropertySources().contains(propertySource.getName()); } diff --git a/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java similarity index 88% rename from module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java rename to module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java index 9c03ed9e43d6..099cf435bcd6 100644 --- a/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTest.java +++ b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java @@ -25,15 +25,15 @@ import org.springframework.core.env.PropertySource; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** - * Test for {@link ManagementContextFactory}. + * Tests for {@link ManagementContextFactory}. * * author Yongjun Hong */ -class ManagementContextFactoryTest { +class ManagementContextFactoryTests { @Test void createManagementContextCopiesManagementPropertySources() { @@ -47,8 +47,8 @@ public Object getProperty(String name) { } }; parentPropertySources.addLast(managementPropertySource); - when(parentEnvironment.getPropertySources()).thenReturn(parentPropertySources); - when(parentContext.getEnvironment()).thenReturn(parentEnvironment); + given(parentEnvironment.getPropertySources()).willReturn(parentPropertySources); + given(parentContext.getEnvironment()).willReturn(parentEnvironment); ManagementContextFactory factory = new ManagementContextFactory(WebApplicationType.SERVLET, null); @@ -57,4 +57,5 @@ public Object getProperty(String name) { ConfigurableEnvironment childEnvironment = managementContext.getEnvironment(); assertThat(childEnvironment.getPropertySources().contains("managementProperty")).isTrue(); } + } \ No newline at end of file From d787085652a14820f86d57c0f0bceba1a2a35cf1 Mon Sep 17 00:00:00 2001 From: yongjunhong Date: Tue, 19 Aug 2025 08:40:49 +0900 Subject: [PATCH 3/3] run format Signed-off-by: yongjunhong --- .../web/server/ManagementContextFactoryTests.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java index 099cf435bcd6..85fd5744b499 100644 --- a/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java +++ b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.autoconfigure.web.server; import org.junit.jupiter.api.Test; + import org.springframework.boot.WebApplicationType; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -58,4 +59,4 @@ public Object getProperty(String name) { assertThat(childEnvironment.getPropertySources().contains("managementProperty")).isTrue(); } -} \ No newline at end of file +}