Merge branch '2.4.x'

Closes gh-26401
This commit is contained in:
Phillip Webb 2021-05-06 19:50:17 -07:00
commit 777abc4aac
8 changed files with 38 additions and 10 deletions

View File

@ -119,6 +119,13 @@ public final class ConfigData {
@FunctionalInterface
public interface PropertySourceOptions {
/**
* {@link PropertySourceOptions} instance that always returns
* {@link Options#NONE}.
* @since 2.4.6
*/
PropertySourceOptions ALWAYS_NONE = new AlwaysPropertySourceOptions(Options.NONE);
/**
* Return the options that should apply for the given property source.
* @param propertySource the property source
@ -143,6 +150,9 @@ public final class ConfigData {
* @return a new {@link PropertySourceOptions} instance
*/
static PropertySourceOptions always(Options options) {
if (options == Options.NONE) {
return ALWAYS_NONE;
}
return new AlwaysPropertySourceOptions(options);
}
@ -176,7 +186,7 @@ public final class ConfigData {
/**
* No options.
*/
public static final Options NONE = Options.of();
public static final Options NONE = new Options(Collections.emptySet());
private final Set<Option> options;
@ -253,8 +263,10 @@ public final class ConfigData {
*/
public static Options of(Option... options) {
Assert.notNull(options, "Options must not be null");
return new Options(
(options.length != 0) ? EnumSet.copyOf(Arrays.asList(options)) : EnumSet.noneOf(Option.class));
if (options.length == 0) {
return NONE;
}
return new Options(EnumSet.copyOf(Arrays.asList(options)));
}
}

View File

@ -458,7 +458,7 @@ class ConfigDataEnvironmentContributor implements Iterable<ConfigDataEnvironment
BOUND_IMPORT,
/**
* A valid location that contained noething to load.
* A valid location that contained nothing to load.
*/
EMPTY_LOCATION;

View File

@ -19,6 +19,8 @@ package org.springframework.boot.context.config;
import java.io.IOException;
import java.util.List;
import org.springframework.boot.context.config.ConfigData.Option;
import org.springframework.boot.context.config.ConfigData.PropertySourceOptions;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginTrackedResource;
import org.springframework.core.env.PropertySource;
@ -33,6 +35,10 @@ import org.springframework.core.io.Resource;
*/
public class StandardConfigDataLoader implements ConfigDataLoader<StandardConfigDataResource> {
private static final PropertySourceOptions PROFILE_SPECIFIC = PropertySourceOptions.always(Option.PROFILE_SPECIFIC);
private static final PropertySourceOptions NON_PROFILE_SPECIFIC = PropertySourceOptions.ALWAYS_NONE;
@Override
public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResource resource)
throws IOException, ConfigDataNotFoundException {
@ -46,7 +52,8 @@ public class StandardConfigDataLoader implements ConfigDataLoader<StandardConfig
String name = String.format("Config resource '%s' via location '%s'", resource,
reference.getConfigDataLocation());
List<PropertySource<?>> propertySources = reference.getPropertySourceLoader().load(name, originTrackedResource);
return new ConfigData(propertySources);
PropertySourceOptions options = (resource.getProfile() != null) ? PROFILE_SPECIFIC : NON_PROFILE_SPECIFIC;
return new ConfigData(propertySources, options);
}
}

View File

@ -27,8 +27,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.logging.log4j.util.Strings;
import org.assertj.core.api.Condition;
@ -407,9 +405,7 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
String property = context.getEnvironment().getProperty("my.property");
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev");
assertThat(property).isEqualTo("fromdevprofile");
List<String> names = StreamSupport.stream(context.getEnvironment().getPropertySources().spliterator(), false)
.map(org.springframework.core.env.PropertySource::getName).collect(Collectors.toList());
assertThat(names).contains(
assertThat(context.getEnvironment().getPropertySources()).extracting("name").contains(
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #0)",
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #1)");
}
@ -600,6 +596,14 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported");
}
@Test
void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-profile-variant.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
}
@Test
void runWhenHasPropertyInProfileDocumentThrowsException() {
assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.application.run(

View File

@ -0,0 +1,2 @@
spring.config.import=classpath:application-import-with-profile-variant-imported.properties
my.value=notimported