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..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 @@ -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,12 @@ 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/ManagementContextFactoryTests.java b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java new file mode 100644 index 000000000000..85fd5744b499 --- /dev/null +++ b/module/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactoryTests.java @@ -0,0 +1,62 @@ +/* + * 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.BDDMockito.given; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link ManagementContextFactory}. + * + * author Yongjun Hong + */ +class ManagementContextFactoryTests { + + @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); + given(parentEnvironment.getPropertySources()).willReturn(parentPropertySources); + given(parentContext.getEnvironment()).willReturn(parentEnvironment); + + ManagementContextFactory factory = new ManagementContextFactory(WebApplicationType.SERVLET, null); + + ConfigurableApplicationContext managementContext = factory.createManagementContext(parentContext); + + ConfigurableEnvironment childEnvironment = managementContext.getEnvironment(); + assertThat(childEnvironment.getPropertySources().contains("managementProperty")).isTrue(); + } + +}