Skip to content

Commit 2dc387f

Browse files
committed
1 parent 2904285 commit 2dc387f

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceLocator.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 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.
@@ -21,8 +21,10 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323

24+
import org.springframework.boot.cloud.CloudPlatform;
2425
import org.springframework.core.env.CompositePropertySource;
2526
import org.springframework.core.env.Environment;
27+
import org.springframework.core.env.Profiles;
2628
import org.springframework.core.env.PropertySource;
2729

2830
/**
@@ -31,6 +33,7 @@
3133
* starting.
3234
*
3335
* @author Dave Syer
36+
* @author Yanming Zhou
3437
*
3538
*/
3639
public interface PropertySourceLocator {
@@ -55,15 +58,29 @@ static Collection<PropertySource<?>> locateCollection(PropertySourceLocator loca
5558
Collection<PropertySource<?>> sources = ((CompositePropertySource) propertySource).getPropertySources();
5659
List<PropertySource<?>> filteredSources = new ArrayList<>();
5760
for (PropertySource<?> p : sources) {
58-
if (p != null) {
61+
if (p != null && shouldActivatePropertySource(p, environment)) {
5962
filteredSources.add(p);
6063
}
6164
}
6265
return filteredSources;
6366
}
6467
else {
65-
return List.of(propertySource);
68+
return shouldActivatePropertySource(propertySource, environment) ? List.of(propertySource)
69+
: Collections.emptyList();
6670
}
6771
}
6872

73+
private static boolean shouldActivatePropertySource(PropertySource<?> ps, Environment environment) {
74+
String onProfile = (String) ps.getProperty("spring.config.activate.on-profile");
75+
if ((onProfile != null) && !environment.acceptsProfiles(Profiles.of(onProfile))) {
76+
return false;
77+
}
78+
String onCloudPlatform = (String) ps.getProperty("spring.config.activate.on-cloud-platform");
79+
if (onCloudPlatform != null) {
80+
CloudPlatform cloudPlatform = CloudPlatform.getActive(environment);
81+
return cloudPlatform != null && cloudPlatform.name().equalsIgnoreCase(onCloudPlatform);
82+
}
83+
return true;
84+
}
85+
6986
}

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

+74-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 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.
@@ -49,6 +49,7 @@
4949

5050
/**
5151
* @author Dave Syer
52+
* @author Yanming Zhou
5253
*
5354
*/
5455
public class BootstrapConfigurationTests {
@@ -740,6 +741,78 @@ void activeAndIncludeProfileFromBootstrapPropertySource_WhenMultiplePlacesHaveAc
740741
.anyMatch("local"::equals)).isTrue();
741742
}
742743

744+
@Test
745+
void activatedOnProfile() {
746+
PropertySourceConfiguration.MAP.put("stage", "dev");
747+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
748+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
749+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
750+
.properties(properties)
751+
.sources(BareConfiguration.class)
752+
.run("--spring.profiles.active=dev");
753+
then(this.context.getEnvironment().getProperty("stage")).isEqualTo("dev");
754+
}
755+
756+
@Test
757+
void notActivatedOnNoActiveProfile() {
758+
PropertySourceConfiguration.MAP.put("stage", "dev");
759+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
760+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
761+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
762+
.properties(properties)
763+
.sources(BareConfiguration.class)
764+
.run();
765+
then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev");
766+
}
767+
768+
@Test
769+
void notActivatedOnMismatchedProfile() {
770+
PropertySourceConfiguration.MAP.put("stage", "dev");
771+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
772+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
773+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
774+
.properties(properties)
775+
.sources(BareConfiguration.class)
776+
.run("--spring.profiles.active=prod");
777+
then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev");
778+
}
779+
780+
@Test
781+
void activatedOnCloudPlatform() {
782+
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
783+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
784+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
785+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
786+
.properties(properties)
787+
.sources(BareConfiguration.class)
788+
.run("--spring.main.cloud-platform=kubernetes");
789+
then(this.context.getEnvironment().getProperty("cloud")).isEqualTo("kubernetes");
790+
}
791+
792+
@Test
793+
void notActivatedOnNoActiveCloudPlatform() {
794+
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
795+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
796+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
797+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
798+
.properties(properties)
799+
.sources(BareConfiguration.class)
800+
.run();
801+
then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes");
802+
}
803+
804+
@Test
805+
void notActivatedOnMismatchedCloudPlatform() {
806+
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
807+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
808+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
809+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
810+
.properties(properties)
811+
.sources(BareConfiguration.class)
812+
.run("--spring.main.cloud-platform=heroku");
813+
then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes");
814+
}
815+
743816
@Configuration(proxyBeanMethods = false)
744817
@EnableConfigurationProperties
745818
protected static class BareConfiguration {

0 commit comments

Comments
 (0)