Skip to content

Commit d4b9786

Browse files
committed
Merge branch '3.5.x'
Closes gh-48177
2 parents ae4bf3e + 4a016ec commit d4b9786

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

core/spring-boot/src/main/java/org/springframework/boot/support/SystemEnvironmentPropertySourceEnvironmentPostProcessor.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* {@link SystemEnvironmentOrigin} for every system environment property.
4141
*
4242
* @author Madhura Bhave
43+
* @author Phillip Webb
4344
* @since 4.0.0
4445
*/
4546
public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
@@ -53,10 +54,14 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements
5354

5455
@Override
5556
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
57+
postProcessEnvironment(environment, application.getEnvironmentPrefix());
58+
}
59+
60+
private void postProcessEnvironment(ConfigurableEnvironment environment, @Nullable String environmentPrefix) {
5661
String sourceName = StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
5762
PropertySource<?> propertySource = environment.getPropertySources().get(sourceName);
5863
if (propertySource != null) {
59-
replacePropertySource(environment, sourceName, propertySource, application.getEnvironmentPrefix());
64+
replacePropertySource(environment, sourceName, propertySource, environmentPrefix);
6065
}
6166
}
6267

@@ -78,6 +83,23 @@ public void setOrder(int order) {
7883
this.order = order;
7984
}
8085

86+
/**
87+
* Post-process the given {@link ConfigurableEnvironment} by copying appropriate
88+
* settings from a parent {@link ConfigurableEnvironment}.
89+
* @param environment the environment to post-process
90+
* @param parentEnvironment the parent environment
91+
* @since 3.4.12
92+
*/
93+
public static void postProcessEnvironment(ConfigurableEnvironment environment,
94+
ConfigurableEnvironment parentEnvironment) {
95+
PropertySource<?> parentSystemEnvironmentPropertySource = parentEnvironment.getPropertySources()
96+
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
97+
if (parentSystemEnvironmentPropertySource instanceof OriginAwareSystemEnvironmentPropertySource parentOriginAwareSystemEnvironmentPropertySource) {
98+
new SystemEnvironmentPropertySourceEnvironmentPostProcessor().postProcessEnvironment(environment,
99+
parentOriginAwareSystemEnvironmentPropertySource.getPrefix());
100+
}
101+
}
102+
81103
/**
82104
* {@link SystemEnvironmentPropertySource} that also tracks {@link Origin}.
83105
*/

core/spring-boot/src/test/java/org/springframework/boot/support/SystemEnvironmentPropertySourceEnvironmentPostProcessorTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,19 @@ void propertySourceShouldBePrefixed() {
111111
assertThat(replaced.getPrefix()).isEqualTo("my");
112112
}
113113

114+
@Test
115+
void postProcessWithParentEnvironmentShouldApplyPrefix() {
116+
SpringApplication application = new SpringApplication();
117+
application.setEnvironmentPrefix("my");
118+
new SystemEnvironmentPropertySourceEnvironmentPostProcessor().postProcessEnvironment(this.environment,
119+
application);
120+
StandardEnvironment child = new StandardEnvironment();
121+
SystemEnvironmentPropertySourceEnvironmentPostProcessor.postProcessEnvironment(child, this.environment);
122+
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) child
123+
.getPropertySources()
124+
.get("systemEnvironment");
125+
assertThat(replaced).isNotNull();
126+
assertThat(replaced.getPrefix()).isEqualTo("my");
127+
}
128+
114129
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public boolean isBeanExcludedFromAotProcessing() {
139139
return false;
140140
}
141141

142+
@Nullable ConfigurableApplicationContext getManagementContext() {
143+
return this.managementContext;
144+
}
145+
142146
private void registerBeans(ConfigurableApplicationContext managementContext) {
143147
if (this.applicationContextInitializer != null) {
144148
this.applicationContextInitializer.initialize(managementContext);

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.support.RootBeanDefinition;
2525
import org.springframework.boot.ApplicationContextFactory;
2626
import org.springframework.boot.WebApplicationType;
27+
import org.springframework.boot.support.SystemEnvironmentPropertySourceEnvironmentPostProcessor;
2728
import org.springframework.boot.web.server.WebServerFactory;
2829
import org.springframework.context.ApplicationContext;
2930
import org.springframework.context.ConfigurableApplicationContext;
@@ -62,8 +63,8 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
6263
ConfigurableEnvironment childEnvironment = ApplicationContextFactory.DEFAULT
6364
.createEnvironment(this.webApplicationType);
6465
Assert.state(childEnvironment != null, "'childEnvironment' must not be null");
65-
if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) {
66-
childEnvironment.setConversionService((configurableEnvironment).getConversionService());
66+
if (parentEnvironment instanceof ConfigurableEnvironment configurableParentEnvironment) {
67+
postProcessChildEnvironment(childEnvironment, configurableParentEnvironment);
6768
}
6869
ConfigurableApplicationContext managementContext = ApplicationContextFactory.DEFAULT
6970
.create(this.webApplicationType);
@@ -73,6 +74,13 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
7374
return managementContext;
7475
}
7576

77+
private void postProcessChildEnvironment(ConfigurableEnvironment childEnvironment,
78+
ConfigurableEnvironment parentEnvironment) {
79+
childEnvironment.setConversionService((parentEnvironment).getConversionService());
80+
SystemEnvironmentPropertySourceEnvironmentPostProcessor.postProcessEnvironment(childEnvironment,
81+
parentEnvironment);
82+
}
83+
7684
public void registerWebServerFactoryBeans(ApplicationContext parentContext,
7785
ConfigurableApplicationContext managementContext, AnnotationConfigRegistry registry) {
7886
if (this.autoConfigurationClasses != null && this.autoConfigurationClasses.length > 0) {

module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/actuate/web/ServletManagementContextAutoConfigurationIntegrationTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,33 @@
1616

1717
package org.springframework.boot.servlet.autoconfigure.actuate.web;
1818

19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
1921
import java.util.function.Consumer;
2022

2123
import org.junit.jupiter.api.Test;
2224
import org.junit.jupiter.api.extension.ExtendWith;
2325

26+
import org.springframework.boot.SpringApplication;
2427
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
2528
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
2629
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
2730
import org.springframework.boot.autoconfigure.AutoConfigurations;
31+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
32+
import org.springframework.boot.env.PropertySourceInfo;
2833
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2934
import org.springframework.boot.test.system.CapturedOutput;
3035
import org.springframework.boot.test.system.OutputCaptureExtension;
3136
import org.springframework.boot.tomcat.autoconfigure.actuate.web.server.TomcatServletManagementContextAutoConfiguration;
3237
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
3338
import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext;
39+
import org.springframework.context.ConfigurableApplicationContext;
40+
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.core.env.ConfigurableEnvironment;
42+
import org.springframework.core.env.PropertySource;
43+
import org.springframework.core.env.StandardEnvironment;
44+
import org.springframework.test.util.ReflectionTestUtils;
45+
import org.springframework.util.ClassUtils;
3446
import org.springframework.util.StringUtils;
3547

3648
import static org.assertj.core.api.Assertions.assertThat;
@@ -100,11 +112,44 @@ void givenSamePortManagementServerWhenManagementServerAddressIsConfiguredThenCon
100112
.hasMessageStartingWith("Management-specific server address cannot be configured"));
101113
}
102114

115+
@Test // gh-45858
116+
void childEnvironmentShouldInheritPrefix() throws Exception {
117+
SpringApplication application = new SpringApplication(ChildEnvironmentConfiguration.class);
118+
Map<String, Object> properties = new LinkedHashMap<>();
119+
properties.put("server.port", "0");
120+
properties.put("management.server.port", "0");
121+
application.setDefaultProperties(properties);
122+
application.setEnvironmentPrefix("my");
123+
try (ConfigurableApplicationContext parentContext = application.run()) {
124+
Class<?> initializerClass = ClassUtils.forName(
125+
"org.springframework.boot.actuate.autoconfigure.web.server.ChildManagementContextInitializer",
126+
null);
127+
Object initializer = parentContext.getBean(initializerClass);
128+
ConfigurableApplicationContext managementContext = (ConfigurableApplicationContext) ReflectionTestUtils
129+
.getField(initializer, "managementContext");
130+
assertThat(managementContext).isNotNull();
131+
ConfigurableEnvironment managementEnvironment = managementContext.getEnvironment();
132+
assertThat(managementEnvironment).isNotNull();
133+
PropertySource<?> systemEnvironmentPropertySource = managementEnvironment.getPropertySources()
134+
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
135+
assertThat(systemEnvironmentPropertySource).isNotNull();
136+
assertThat(((PropertySourceInfo) systemEnvironmentPropertySource).getPrefix()).isEqualTo("my");
137+
}
138+
}
139+
103140
private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) {
104141
return (charSequence) -> {
105142
int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);
106143
assertThat(count).isEqualTo(expectedCount);
107144
};
108145
}
109146

147+
@Configuration(proxyBeanMethods = false)
148+
@ImportAutoConfiguration({ ManagementContextAutoConfiguration.class, TomcatServletWebServerAutoConfiguration.class,
149+
TomcatServletManagementContextAutoConfiguration.class, ServletManagementContextAutoConfiguration.class,
150+
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class })
151+
static class ChildEnvironmentConfiguration {
152+
153+
}
154+
110155
}

0 commit comments

Comments
 (0)