Import profile specific files in correct order
Update `StandardConfigDataLoader` to mark profile specific files with
`Option.PROFILE` so that they are added in the correct order. This is
a variation of the same issue described in commit 5774ea3f0c
.
Closes gh-26400
Co-authored-by: Scott Frederick <sfrederick@vmware.com>
Co-authored-by: Madhura Bhave <mbhave@vmware.com>
This commit is contained in:
parent
922517c0e5
commit
fef62f784a
|
@ -118,6 +118,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
|
||||
|
@ -142,6 +149,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);
|
||||
}
|
||||
|
||||
|
@ -175,7 +185,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;
|
||||
|
||||
|
@ -238,8 +248,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)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -596,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(
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
my.value=notimported-dev
|
|
@ -0,0 +1 @@
|
|||
my.value=iwasimported-dev
|
|
@ -0,0 +1 @@
|
|||
my.value=iwasimported
|
|
@ -0,0 +1,2 @@
|
|||
spring.config.import=classpath:application-import-with-profile-variant-imported.properties
|
||||
my.value=notimported
|
Loading…
Reference in New Issue