Merge branch '2.4.x'

Closes gh-26402
pull/26416/head
Phillip Webb 4 years ago
commit ba1e5fb5e8

@ -233,7 +233,7 @@ class ConfigDataEnvironment {
contributors = processWithoutProfiles(contributors, importer, activationContext);
activationContext = withProfiles(contributors, activationContext);
contributors = processWithProfiles(contributors, importer, activationContext);
applyToEnvironment(contributors, activationContext);
applyToEnvironment(contributors, activationContext, importer.getLoadedLocations());
}
private ConfigDataEnvironmentContributors processInitial(ConfigDataEnvironmentContributors contributors,
@ -320,9 +320,9 @@ class ConfigDataEnvironment {
}
private void applyToEnvironment(ConfigDataEnvironmentContributors contributors,
ConfigDataActivationContext activationContext) {
ConfigDataActivationContext activationContext, Set<ConfigDataLocation> loadedLocations) {
checkForInvalidProperties(contributors);
checkMandatoryLocations(contributors, activationContext);
checkMandatoryLocations(contributors, activationContext, loadedLocations);
MutablePropertySources propertySources = this.environment.getPropertySources();
this.logger.trace("Applying config data environment contributions");
for (ConfigDataEnvironmentContributor contributor : contributors) {
@ -357,7 +357,7 @@ class ConfigDataEnvironment {
}
private void checkMandatoryLocations(ConfigDataEnvironmentContributors contributors,
ConfigDataActivationContext activationContext) {
ConfigDataActivationContext activationContext, Set<ConfigDataLocation> loadedLocations) {
Set<ConfigDataLocation> mandatoryLocations = new LinkedHashSet<>();
for (ConfigDataEnvironmentContributor contributor : contributors) {
if (contributor.isActive(activationContext)) {
@ -369,6 +369,7 @@ class ConfigDataEnvironment {
mandatoryLocations.remove(contributor.getLocation());
}
}
mandatoryLocations.removeAll(loadedLocations);
if (!mandatoryLocations.isEmpty()) {
for (ConfigDataLocation mandatoryLocation : mandatoryLocations) {
this.notFoundAction.handle(this.logger, new ConfigDataLocationNotFoundException(mandatoryLocation));

@ -49,6 +49,8 @@ class ConfigDataImporter {
private final Set<ConfigDataResource> loaded = new HashSet<>();
private final Set<ConfigDataLocation> loadedLocations = new HashSet<>();
/**
* Create a new {@link ConfigDataImporter} instance.
* @param logFactory the log factory
@ -113,10 +115,15 @@ class ConfigDataImporter {
ConfigDataResolutionResult candidate = candidates.get(i);
ConfigDataLocation location = candidate.getLocation();
ConfigDataResource resource = candidate.getResource();
if (this.loaded.add(resource)) {
if (this.loaded.contains(resource)) {
this.loadedLocations.add(location);
}
else {
try {
ConfigData loaded = this.loaders.load(loaderContext, resource);
if (loaded != null) {
this.loaded.add(resource);
this.loadedLocations.add(location);
result.put(candidate, loaded);
}
}
@ -139,4 +146,8 @@ class ConfigDataImporter {
return (!location.isOptional()) ? this.notFoundAction : ConfigDataNotFoundAction.IGNORE;
}
Set<ConfigDataLocation> getLoadedLocations() {
return this.loadedLocations;
}
}

@ -604,6 +604,14 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
}
@Test
void runWhenImportWithProfileVariantAndDirectProfileImportOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=classpath:application-import-with-profile-variant-and-direct-profile-import.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
}
@Test
void runWhenHasPropertyInProfileDocumentThrowsException() {
assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.application.run(

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

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