Skip to content

Commit ba4eec8

Browse files
Continue processing properties files on error
Prior to this commit, processing of multiple properties files discovered using a wildcard pattern would stop on any error encountered while processing a file or when an empty properties file was found, causing subsequent files from the pattern to be ignored. This commit changes the behavior such that subsequent files are processed on error or on an empty file. Fixes gh-20873
1 parent 5afcaa7 commit ba4eec8

File tree

7 files changed

+21
-19
lines changed

7 files changed

+21
-19
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,24 +503,24 @@ private void loadForFileExtension(PropertySourceLoader loader, String prefix, St
503503

504504
private void load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter,
505505
DocumentConsumer consumer) {
506-
try {
507-
Resource[] resources = getResources(location);
508-
for (Resource resource : resources) {
506+
Resource[] resources = getResources(location);
507+
for (Resource resource : resources) {
508+
try {
509509
if (resource == null || !resource.exists()) {
510510
if (this.logger.isTraceEnabled()) {
511511
StringBuilder description = getDescription("Skipped missing config ", location, resource,
512512
profile);
513513
this.logger.trace(description);
514514
}
515-
return;
515+
continue;
516516
}
517517
if (!StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename()))) {
518518
if (this.logger.isTraceEnabled()) {
519519
StringBuilder description = getDescription("Skipped empty config extension ", location,
520520
resource, profile);
521521
this.logger.trace(description);
522522
}
523-
return;
523+
continue;
524524
}
525525
String name = (location.contains("*")) ? "applicationConfig: [" + resource.toString() + "]"
526526
: "applicationConfig: [" + location + "]";
@@ -531,7 +531,7 @@ private void load(PropertySourceLoader loader, String location, Profile profile,
531531
profile);
532532
this.logger.trace(description);
533533
}
534-
return;
534+
continue;
535535
}
536536
List<Document> loaded = new ArrayList<>();
537537
for (Document document : documents) {
@@ -551,9 +551,11 @@ private void load(PropertySourceLoader loader, String location, Profile profile,
551551
}
552552
}
553553
}
554-
}
555-
catch (Exception ex) {
556-
throw new IllegalStateException("Failed to load property source from location '" + location + "'", ex);
554+
catch (Exception ex) {
555+
StringBuilder description = getDescription("Failed to load property source from ", location,
556+
resource, profile);
557+
throw new IllegalStateException(description.toString(), ex);
558+
}
557559
}
558560
}
559561

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,10 +1010,10 @@ void locationsWithWildcardFoldersShouldLoadAllFilesThatMatch() {
10101010
"spring.config.location=" + location);
10111011
this.initializer.setSearchNames("testproperties");
10121012
this.initializer.postProcessEnvironment(this.environment, this.application);
1013-
String a = this.environment.getProperty("a.property");
1014-
String b = this.environment.getProperty("b.property");
1015-
assertThat(a).isEqualTo("apple");
1016-
assertThat(b).isEqualTo("ball");
1013+
String first = this.environment.getProperty("first.property");
1014+
String second = this.environment.getProperty("second.property");
1015+
assertThat(first).isEqualTo("apple");
1016+
assertThat(second).isEqualTo("ball");
10171017
}
10181018

10191019
@Test
@@ -1023,10 +1023,10 @@ void locationsWithWildcardFilesShouldLoadAllFilesThatMatch() {
10231023
"spring.config.location=" + location);
10241024
this.initializer.setSearchNames("testproperties");
10251025
this.initializer.postProcessEnvironment(this.environment, this.application);
1026-
String a = this.environment.getProperty("a.property");
1027-
String b = this.environment.getProperty("b.property");
1028-
assertThat(a).isEqualTo("apple");
1029-
assertThat(b).isEqualTo("ball");
1026+
String first = this.environment.getProperty("first.property");
1027+
String second = this.environment.getProperty("second.property");
1028+
assertThat(first).isEqualTo("apple");
1029+
assertThat(second).isEqualTo("ball");
10301030
}
10311031

10321032
private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {

spring-boot-project/spring-boot/src/test/resources/config/0-empty/testproperties.properties

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
first.property=apple
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
second.property=ball

spring-boot-project/spring-boot/src/test/resources/config/a/testproperties.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-boot-project/spring-boot/src/test/resources/config/b/testproperties.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)