diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java index 4d74198c1e2..1822947978f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -29,6 +29,7 @@ import java.util.stream.StreamSupport; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.PlaceholdersResolver; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; +import org.springframework.boot.origin.OriginLookup; import org.springframework.core.convert.ConversionService; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; @@ -414,7 +415,7 @@ class ConfigDataEnvironmentContributor implements Iterable propertySource, ConversionService conversionService) { return new ConfigDataEnvironmentContributor(Kind.EXISTING, null, null, false, propertySource, - ConfigurationPropertySource.from(propertySource), null, null, null, conversionService); + asConfigurationPropertySource(propertySource), null, null, null, conversionService); } /** @@ -434,9 +435,16 @@ class ConfigDataEnvironmentContributor implements Iterable propertySource = configData.getPropertySources().get(propertySourceIndex); ConfigData.Options options = configData.getOptions(propertySource); - ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource); return new ConfigDataEnvironmentContributor(Kind.UNBOUND_IMPORT, location, resource, profileSpecific, - propertySource, configurationPropertySource, null, options, null, conversionService); + propertySource, asConfigurationPropertySource(propertySource), null, options, null, conversionService); + } + + private static ConfigurationPropertySource asConfigurationPropertySource(PropertySource propertySource) { + ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource); + if (configurationPropertySource != null && propertySource instanceof OriginLookup originLookup) { + configurationPropertySource = configurationPropertySource.withPrefix(originLookup.getPrefix()); + } + return configurationPropertySource; } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/TestApplicationEnvironment.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/TestApplicationEnvironment.java new file mode 100644 index 00000000000..62868352d32 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/TestApplicationEnvironment.java @@ -0,0 +1,26 @@ +/* + * Copyright 2012-2025 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; + +/** + * Public version {@link ApplicationEnvironment} for tests to use. + * + * @author Phillip Webb + */ +public class TestApplicationEnvironment extends ApplicationEnvironment { + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 12005f44db1..0b1c19ba415 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -38,6 +38,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.springframework.boot.SpringApplication; +import org.springframework.boot.TestApplicationEnvironment; import org.springframework.boot.WebApplicationType; import org.springframework.boot.context.properties.bind.BindContext; import org.springframework.boot.context.properties.bind.BindException; @@ -461,6 +462,21 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { assertThat(context.getEnvironment().getProperty("my.property")).isEqualTo("fromotherpropertiesfile"); } + @Test // gh-45387 + void runWhenProfileActivatedViaSystemEnvironmentVariableWithPrefix() { + this.application.setEnvironmentPrefix("example.prefix"); + this.application.setEnvironment(new TestApplicationEnvironment() { + + @Override + public Map getSystemEnvironment() { + return Map.of("EXAMPLE_PREFIX_SPRING_PROFILES_ACTIVE", "other,dev"); + } + + }); + ConfigurableApplicationContext context = this.application.run(); + assertThat(context.getEnvironment().getActiveProfiles()).contains("dev", "other"); + } + @Test @WithResource(name = "application.yaml", content = """ ---