Skip to content

Commit 4a016ec

Browse files
committed
Merge branch '3.4.x' into 3.5.x
Closes gh-48176
2 parents 45f4da7 + 3040dc1 commit 4a016ec

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/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.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor;
2728
import org.springframework.boot.web.server.WebServerFactory;
2829
import org.springframework.context.ApplicationContext;
2930
import org.springframework.context.ConfigurableApplicationContext;
@@ -60,8 +61,8 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
6061
Environment parentEnvironment = parentContext.getEnvironment();
6162
ConfigurableEnvironment childEnvironment = ApplicationContextFactory.DEFAULT
6263
.createEnvironment(this.webApplicationType);
63-
if (parentEnvironment instanceof ConfigurableEnvironment configurableEnvironment) {
64-
childEnvironment.setConversionService((configurableEnvironment).getConversionService());
64+
if (parentEnvironment instanceof ConfigurableEnvironment configurableParentEnvironment) {
65+
postProcessChildEnvironment(childEnvironment, configurableParentEnvironment);
6566
}
6667
ConfigurableApplicationContext managementContext = ApplicationContextFactory.DEFAULT
6768
.create(this.webApplicationType);
@@ -70,6 +71,13 @@ public ConfigurableApplicationContext createManagementContext(ApplicationContext
7071
return managementContext;
7172
}
7273

74+
private void postProcessChildEnvironment(ConfigurableEnvironment childEnvironment,
75+
ConfigurableEnvironment parentEnvironment) {
76+
childEnvironment.setConversionService((parentEnvironment).getConversionService());
77+
SystemEnvironmentPropertySourceEnvironmentPostProcessor.postProcessEnvironment(childEnvironment,
78+
parentEnvironment);
79+
}
80+
7381
public void registerWebServerFactoryBeans(ApplicationContext parentContext,
7482
ConfigurableApplicationContext managementContext, AnnotationConfigRegistry registry) {
7583
registry.register(this.autoConfigurationClasses);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public int getPhase() {
121121
@Override
122122
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
123123
Assert.isInstanceOf(ConfigurableApplicationContext.class, this.parentContext);
124-
BeanFactory parentBeanFactory = ((ConfigurableApplicationContext) this.parentContext).getBeanFactory();
124+
BeanFactory parentBeanFactory = this.parentContext.getBeanFactory();
125125
if (registeredBean.getBeanClass().equals(getClass())
126126
&& registeredBean.getBeanFactory().equals(parentBeanFactory)) {
127127
ConfigurableApplicationContext managementContext = createManagementContext();
@@ -136,6 +136,10 @@ public boolean isBeanExcludedFromAotProcessing() {
136136
return false;
137137
}
138138

139+
ConfigurableApplicationContext getManagementContext() {
140+
return this.managementContext;
141+
}
142+
139143
private void registerBeans(ConfigurableApplicationContext managementContext) {
140144
if (this.applicationContextInitializer != null) {
141145
this.applicationContextInitializer.initialize(managementContext);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java

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

1717
package org.springframework.boot.actuate.autoconfigure.web.server;
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.servlet.ServletManagementContextAutoConfiguration;
2730
import org.springframework.boot.autoconfigure.AutoConfigurations;
31+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2832
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
2933
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
34+
import org.springframework.boot.origin.OriginLookup;
3035
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
3136
import org.springframework.boot.test.system.CapturedOutput;
3237
import org.springframework.boot.test.system.OutputCaptureExtension;
3338
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
39+
import org.springframework.context.ConfigurableApplicationContext;
40+
import org.springframework.context.annotation.Configuration;
41+
import org.springframework.core.env.PropertySource;
42+
import org.springframework.core.env.StandardEnvironment;
3443
import org.springframework.util.StringUtils;
3544

3645
import static org.assertj.core.api.Assertions.assertThat;
@@ -95,11 +104,38 @@ void givenSamePortManagementServerWhenManagementServerAddressIsConfiguredThenCon
95104
.hasMessageStartingWith("Management-specific server address cannot be configured"));
96105
}
97106

107+
@Test // gh-45858
108+
void childEnvironmentShouldInheritPrefix() {
109+
SpringApplication application = new SpringApplication(ChildEnvironmentConfiguration.class);
110+
Map<String, Object> properties = new LinkedHashMap<>();
111+
properties.put("server.port", "0");
112+
properties.put("management.server.port", "0");
113+
application.setDefaultProperties(properties);
114+
application.setEnvironmentPrefix("my");
115+
try (ConfigurableApplicationContext parentContext = application.run()) {
116+
ChildManagementContextInitializer initializer = parentContext
117+
.getBean(ChildManagementContextInitializer.class);
118+
ConfigurableApplicationContext managementContext = initializer.getManagementContext();
119+
PropertySource<?> systemEnvironmentPropertySource = managementContext.getEnvironment()
120+
.getPropertySources()
121+
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
122+
assertThat(((OriginLookup<?>) systemEnvironmentPropertySource).getPrefix()).isEqualTo("my");
123+
}
124+
}
125+
98126
private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) {
99127
return (charSequence) -> {
100128
int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);
101129
assertThat(count).isEqualTo(expectedCount);
102130
};
103131
}
104132

133+
@Configuration(proxyBeanMethods = false)
134+
@ImportAutoConfiguration({ ManagementContextAutoConfiguration.class, ServletWebServerFactoryAutoConfiguration.class,
135+
ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
136+
EndpointAutoConfiguration.class, DispatcherServletAutoConfiguration.class })
137+
static class ChildEnvironmentConfiguration {
138+
139+
}
140+
105141
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SystemEnvironmentPropertySourceEnvironmentPostProcessor.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* {@link SystemEnvironmentOrigin} for every system environment property.
3737
*
3838
* @author Madhura Bhave
39+
* @author Phillip Webb
3940
* @since 2.0.0
4041
*/
4142
public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
@@ -49,10 +50,14 @@ public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements
4950

5051
@Override
5152
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
53+
postProcessEnvironment(environment, application.getEnvironmentPrefix());
54+
}
55+
56+
private void postProcessEnvironment(ConfigurableEnvironment environment, String environmentPrefix) {
5257
String sourceName = StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
5358
PropertySource<?> propertySource = environment.getPropertySources().get(sourceName);
5459
if (propertySource != null) {
55-
replacePropertySource(environment, sourceName, propertySource, application.getEnvironmentPrefix());
60+
replacePropertySource(environment, sourceName, propertySource, environmentPrefix);
5661
}
5762
}
5863

@@ -74,6 +79,23 @@ public void setOrder(int order) {
7479
this.order = order;
7580
}
7681

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

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SystemEnvironmentPropertySourceEnvironmentPostProcessorTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,18 @@ void propertySourceShouldBePrefixed() {
105105
assertThat(replaced.getPrefix()).isEqualTo("my");
106106
}
107107

108+
@Test
109+
void postProcessWithParentEnvironmentShouldApplyPrefix() {
110+
SpringApplication application = new SpringApplication();
111+
application.setEnvironmentPrefix("my");
112+
new SystemEnvironmentPropertySourceEnvironmentPostProcessor().postProcessEnvironment(this.environment,
113+
application);
114+
StandardEnvironment child = new StandardEnvironment();
115+
SystemEnvironmentPropertySourceEnvironmentPostProcessor.postProcessEnvironment(child, this.environment);
116+
OriginAwareSystemEnvironmentPropertySource replaced = (OriginAwareSystemEnvironmentPropertySource) child
117+
.getPropertySources()
118+
.get("systemEnvironment");
119+
assertThat(replaced.getPrefix()).isEqualTo("my");
120+
}
121+
108122
}

0 commit comments

Comments
 (0)