Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertySourceLocator should honer spring.config.activate.* #1465

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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.
Expand All @@ -21,8 +21,10 @@
import java.util.Collections;
import java.util.List;

import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.core.env.PropertySource;

/**
Expand All @@ -31,6 +33,7 @@
* starting.
*
* @author Dave Syer
* @author Yanming Zhou
*
*/
public interface PropertySourceLocator {
Expand All @@ -55,15 +58,29 @@ static Collection<PropertySource<?>> locateCollection(PropertySourceLocator loca
Collection<PropertySource<?>> sources = ((CompositePropertySource) propertySource).getPropertySources();
List<PropertySource<?>> filteredSources = new ArrayList<>();
for (PropertySource<?> p : sources) {
if (p != null) {
if (p != null && shouldActivatePropertySource(p, environment)) {
filteredSources.add(p);
}
}
return filteredSources;
}
else {
return List.of(propertySource);
return shouldActivatePropertySource(propertySource, environment) ? List.of(propertySource)
: Collections.emptyList();
}
}

private static boolean shouldActivatePropertySource(PropertySource<?> ps, Environment environment) {
String onProfile = (String) ps.getProperty("spring.config.activate.on-profile");
if ((onProfile != null) && !environment.acceptsProfiles(Profiles.of(onProfile))) {
return false;
}
String onCloudPlatform = (String) ps.getProperty("spring.config.activate.on-cloud-platform");
if (onCloudPlatform != null) {
CloudPlatform cloudPlatform = CloudPlatform.getActive(environment);
return cloudPlatform != null && cloudPlatform.name().equalsIgnoreCase(onCloudPlatform);
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -49,6 +49,7 @@

/**
* @author Dave Syer
* @author Yanming Zhou
*
*/
public class BootstrapConfigurationTests {
Expand Down Expand Up @@ -740,6 +741,78 @@ void activeAndIncludeProfileFromBootstrapPropertySource_WhenMultiplePlacesHaveAc
.anyMatch("local"::equals)).isTrue();
}

@Test
void activatedOnProfile() {
PropertySourceConfiguration.MAP.put("stage", "dev");
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run("--spring.profiles.active=dev");
then(this.context.getEnvironment().getProperty("stage")).isEqualTo("dev");
}

@Test
void notActivatedOnNoActiveProfile() {
PropertySourceConfiguration.MAP.put("stage", "dev");
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run();
then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev");
}

@Test
void notActivatedOnMismatchedProfile() {
PropertySourceConfiguration.MAP.put("stage", "dev");
PropertySourceConfiguration.MAP.put("spring.config.activate.on-profile", "dev");
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run("--spring.profiles.active=prod");
then(this.context.getEnvironment().getProperty("stage")).isNotEqualTo("dev");
}

@Test
void activatedOnCloudPlatform() {
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run("--spring.main.cloud-platform=kubernetes");
then(this.context.getEnvironment().getProperty("cloud")).isEqualTo("kubernetes");
}

@Test
void notActivatedOnNoActiveCloudPlatform() {
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run();
then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes");
}

@Test
void notActivatedOnMismatchedCloudPlatform() {
PropertySourceConfiguration.MAP.put("cloud", "kubernetes");
PropertySourceConfiguration.MAP.put("spring.config.activate.on-cloud-platform", "kubernetes");
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties(properties)
.sources(BareConfiguration.class)
.run("--spring.main.cloud-platform=heroku");
then(this.context.getEnvironment().getProperty("cloud")).isNotEqualTo("kubernetes");
}

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
protected static class BareConfiguration {
Expand Down
Loading