Skip to content

Commit f198c86

Browse files
committed
1 parent 06b2d21 commit f198c86

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
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

+72
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,78 @@ public void bootstrapPropertiesWithActivateOnProfile() {
758758
then(this.context.getEnvironment().getProperty("info.name")).isEqualTo("externalPropertiesInfoName from bar");
759759
}
760760

761+
@Test
762+
void activatedOnProfile() {
763+
PropertySourceConfiguration.MAP.put("stage", "dev");
764+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
765+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
766+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
767+
.properties(properties)
768+
.sources(BareConfiguration.class)
769+
.run("--spring.profiles.active=dev");
770+
then(this.context.getEnvironment().getProperty("stage")).isEqualTo("dev");
771+
}
772+
773+
@Test
774+
void notActivatedOnNoActiveProfile() {
775+
PropertySourceConfiguration.MAP.put("stage", "dev");
776+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
777+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
778+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
779+
.properties(properties)
780+
.sources(BareConfiguration.class)
781+
.run();
782+
then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev");
783+
}
784+
785+
@Test
786+
void notActivatedOnMismatchedProfile() {
787+
PropertySourceConfiguration.MAP.put("stage", "dev");
788+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
789+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
790+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
791+
.properties(properties)
792+
.sources(BareConfiguration.class)
793+
.run("--spring.profiles.active=prod");
794+
then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev");
795+
}
796+
797+
@Test
798+
void activatedOnCloudPlatform() {
799+
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
800+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
801+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
802+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
803+
.properties(properties)
804+
.sources(BareConfiguration.class)
805+
.run("--spring.main.cloud-platform=kubernetes");
806+
then(this.context.getEnvironment().getProperty("cloud")).isEqualTo("kubernetes");
807+
}
808+
809+
@Test
810+
void notActivatedOnNoActiveCloudPlatform() {
811+
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
812+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
813+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
814+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
815+
.properties(properties)
816+
.sources(BareConfiguration.class)
817+
.run();
818+
then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes");
819+
}
820+
821+
@Test
822+
void notActivatedOnMismatchedCloudPlatform() {
823+
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
824+
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
825+
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
826+
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
827+
.properties(properties)
828+
.sources(BareConfiguration.class)
829+
.run("--spring.main.cloud-platform=heroku");
830+
then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes");
831+
}
832+
761833
@Configuration(proxyBeanMethods = false)
762834
@EnableConfigurationProperties
763835
protected static class BareConfiguration {

0 commit comments

Comments
 (0)