Skip to content

Commit e0a23e5

Browse files
committed
Merge branch '3.4.x'
Closes gh-45549
2 parents e1ff4fb + c2d2c95 commit e0a23e5

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.context.properties.bind.Binder;
3030
import org.springframework.boot.context.properties.bind.PlaceholdersResolver;
3131
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
32+
import org.springframework.boot.origin.OriginLookup;
3233
import org.springframework.core.convert.ConversionService;
3334
import org.springframework.core.env.Environment;
3435
import org.springframework.core.env.PropertySource;
@@ -414,7 +415,7 @@ static ConfigDataEnvironmentContributor ofInitialImport(ConfigDataLocation initi
414415
static ConfigDataEnvironmentContributor ofExisting(PropertySource<?> propertySource,
415416
ConversionService conversionService) {
416417
return new ConfigDataEnvironmentContributor(Kind.EXISTING, null, null, false, propertySource,
417-
ConfigurationPropertySource.from(propertySource), null, null, null, conversionService);
418+
asConfigurationPropertySource(propertySource), null, null, null, conversionService);
418419
}
419420

420421
/**
@@ -434,9 +435,16 @@ static ConfigDataEnvironmentContributor ofUnboundImport(ConfigDataLocation locat
434435
ConversionService conversionService) {
435436
PropertySource<?> propertySource = configData.getPropertySources().get(propertySourceIndex);
436437
ConfigData.Options options = configData.getOptions(propertySource);
437-
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
438438
return new ConfigDataEnvironmentContributor(Kind.UNBOUND_IMPORT, location, resource, profileSpecific,
439-
propertySource, configurationPropertySource, null, options, null, conversionService);
439+
propertySource, asConfigurationPropertySource(propertySource), null, options, null, conversionService);
440+
}
441+
442+
private static ConfigurationPropertySource asConfigurationPropertySource(PropertySource<?> propertySource) {
443+
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
444+
if (configurationPropertySource != null && propertySource instanceof OriginLookup<?> originLookup) {
445+
configurationPropertySource = configurationPropertySource.withPrefix(originLookup.getPrefix());
446+
}
447+
return configurationPropertySource;
440448
}
441449

442450
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2012-2025 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;
18+
19+
/**
20+
* Public version {@link ApplicationEnvironment} for tests to use.
21+
*
22+
* @author Phillip Webb
23+
*/
24+
public class TestApplicationEnvironment extends ApplicationEnvironment {
25+
26+
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.junit.jupiter.api.io.TempDir;
3939

4040
import org.springframework.boot.SpringApplication;
41+
import org.springframework.boot.TestApplicationEnvironment;
4142
import org.springframework.boot.WebApplicationType;
4243
import org.springframework.boot.context.properties.bind.BindContext;
4344
import org.springframework.boot.context.properties.bind.BindException;
@@ -461,6 +462,21 @@ void runWhenProfilesActivatedViaBracketNotationSetsProfiles() {
461462
assertThat(context.getEnvironment().getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
462463
}
463464

465+
@Test // gh-45387
466+
void runWhenProfileActivatedViaSystemEnvironmentVariableWithPrefix() {
467+
this.application.setEnvironmentPrefix("example.prefix");
468+
this.application.setEnvironment(new TestApplicationEnvironment() {
469+
470+
@Override
471+
public Map<String, Object> getSystemEnvironment() {
472+
return Map.of("EXAMPLE_PREFIX_SPRING_PROFILES_ACTIVE", "other,dev");
473+
}
474+
475+
});
476+
ConfigurableApplicationContext context = this.application.run();
477+
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev", "other");
478+
}
479+
464480
@Test
465481
@WithResource(name = "application.yaml", content = """
466482
---

0 commit comments

Comments
 (0)