Skip to content

Commit 20a225c

Browse files
committed
Fix missing environment prefix in child management context
Signed-off-by: yongjunhong <[email protected]>
1 parent 6b2837c commit 20a225c

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

module/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextFactory.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.context.annotation.AnnotationConfigRegistry;
3131
import org.springframework.core.env.ConfigurableEnvironment;
3232
import org.springframework.core.env.Environment;
33+
import org.springframework.core.env.PropertySource;
3334
import org.springframework.util.Assert;
3435

3536
/**
@@ -40,6 +41,7 @@
4041
*
4142
* @author Andy Wilkinson
4243
* @author Phillip Webb
44+
* @author Yongjun Hong
4345
* @since 4.0.0
4446
*/
4547
public final class ManagementContextFactory {
@@ -63,7 +65,11 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
6365
.createEnvironment(this.webApplicationType);
6466
Assert.state(childEnvironment != null, "'childEnvironment' must not be null");
6567
if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) {
66-
childEnvironment.setConversionService((configurableEnvironment).getConversionService());
68+
configurableEnvironment.getPropertySources().forEach(propertySource -> {
69+
if (isManagementPropertySource(propertySource, childEnvironment)) {
70+
childEnvironment.getPropertySources().addLast(propertySource);
71+
}
72+
});
6773
}
6874
ConfigurableApplicationContext managementContext = ApplicationContextFactory.DEFAULT
6975
.create(this.webApplicationType);
@@ -73,6 +79,11 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
7379
return managementContext;
7480
}
7581

82+
private boolean isManagementPropertySource(PropertySource<?> propertySource, ConfigurableEnvironment childEnvironment) {
83+
return propertySource.getName().contains("management")
84+
&& !childEnvironment.getPropertySources().contains(propertySource.getName());
85+
}
86+
7687
public void registerWebServerFactoryBeans(ApplicationContext parentContext,
7788
ConfigurableApplicationContext managementContext, AnnotationConfigRegistry registry) {
7889
if (this.autoConfigurationClasses != null && this.autoConfigurationClasses.length > 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.server;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.boot.WebApplicationType;
21+
import org.springframework.context.ApplicationContext;
22+
import org.springframework.context.ConfigurableApplicationContext;
23+
import org.springframework.core.env.ConfigurableEnvironment;
24+
import org.springframework.core.env.MutablePropertySources;
25+
import org.springframework.core.env.PropertySource;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
/**
32+
* Test for {@link ManagementContextFactory}.
33+
*
34+
* author Yongjun Hong
35+
*/
36+
class ManagementContextFactoryTest {
37+
38+
@Test
39+
void createManagementContextCopiesManagementPropertySources() {
40+
ApplicationContext parentContext = mock(ApplicationContext.class);
41+
ConfigurableEnvironment parentEnvironment = mock(ConfigurableEnvironment.class);
42+
MutablePropertySources parentPropertySources = new MutablePropertySources();
43+
PropertySource<?> managementPropertySource = new PropertySource<>("managementProperty") {
44+
@Override
45+
public Object getProperty(String name) {
46+
return null;
47+
}
48+
};
49+
parentPropertySources.addLast(managementPropertySource);
50+
when(parentEnvironment.getPropertySources()).thenReturn(parentPropertySources);
51+
when(parentContext.getEnvironment()).thenReturn(parentEnvironment);
52+
53+
ManagementContextFactory factory = new ManagementContextFactory(WebApplicationType.SERVLET, null);
54+
55+
ConfigurableApplicationContext managementContext = factory.createManagementContext(parentContext);
56+
57+
ConfigurableEnvironment childEnvironment = managementContext.getEnvironment();
58+
assertThat(childEnvironment.getPropertySources().contains("managementProperty")).isTrue();
59+
}
60+
}

0 commit comments

Comments
 (0)