From ba4eec87e4d0b06a6d6b1655dcd24446af12127a Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Tue, 7 Apr 2020 15:43:43 -0500 Subject: [PATCH] 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 --- .../config/ConfigFileApplicationListener.java | 20 ++++++++++--------- .../ConfigFileApplicationListenerTests.java | 16 +++++++-------- .../config/0-empty/testproperties.properties | 0 .../config/1-first/testproperties.properties | 1 + .../config/2-second/testproperties.properties | 1 + .../config/a/testproperties.properties | 1 - .../config/b/testproperties.properties | 1 - 7 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/config/0-empty/testproperties.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/config/1-first/testproperties.properties create mode 100644 spring-boot-project/spring-boot/src/test/resources/config/2-second/testproperties.properties delete mode 100644 spring-boot-project/spring-boot/src/test/resources/config/a/testproperties.properties delete mode 100644 spring-boot-project/spring-boot/src/test/resources/config/b/testproperties.properties diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 897b3cc61e..6d45f157df 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -503,16 +503,16 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, private void load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter, DocumentConsumer consumer) { - try { - Resource[] resources = getResources(location); - for (Resource resource : resources) { + Resource[] resources = getResources(location); + for (Resource resource : resources) { + try { if (resource == null || !resource.exists()) { if (this.logger.isTraceEnabled()) { StringBuilder description = getDescription("Skipped missing config ", location, resource, profile); this.logger.trace(description); } - return; + continue; } if (!StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename()))) { if (this.logger.isTraceEnabled()) { @@ -520,7 +520,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, resource, profile); this.logger.trace(description); } - return; + continue; } String name = (location.contains("*")) ? "applicationConfig: [" + resource.toString() + "]" : "applicationConfig: [" + location + "]"; @@ -531,7 +531,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, profile); this.logger.trace(description); } - return; + continue; } List loaded = new ArrayList<>(); for (Document document : documents) { @@ -551,9 +551,11 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } } } - } - catch (Exception ex) { - throw new IllegalStateException("Failed to load property source from location '" + location + "'", ex); + catch (Exception ex) { + StringBuilder description = getDescription("Failed to load property source from ", location, + resource, profile); + throw new IllegalStateException(description.toString(), ex); + } } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 3feb089fe8..0816a41e0b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -1010,10 +1010,10 @@ class ConfigFileApplicationListenerTests { "spring.config.location=" + location); this.initializer.setSearchNames("testproperties"); this.initializer.postProcessEnvironment(this.environment, this.application); - String a = this.environment.getProperty("a.property"); - String b = this.environment.getProperty("b.property"); - assertThat(a).isEqualTo("apple"); - assertThat(b).isEqualTo("ball"); + String first = this.environment.getProperty("first.property"); + String second = this.environment.getProperty("second.property"); + assertThat(first).isEqualTo("apple"); + assertThat(second).isEqualTo("ball"); } @Test @@ -1023,10 +1023,10 @@ class ConfigFileApplicationListenerTests { "spring.config.location=" + location); this.initializer.setSearchNames("testproperties"); this.initializer.postProcessEnvironment(this.environment, this.application); - String a = this.environment.getProperty("a.property"); - String b = this.environment.getProperty("b.property"); - assertThat(a).isEqualTo("apple"); - assertThat(b).isEqualTo("ball"); + String first = this.environment.getProperty("first.property"); + String second = this.environment.getProperty("second.property"); + assertThat(first).isEqualTo("apple"); + assertThat(second).isEqualTo("ball"); } private Condition matchingPropertySource(final String sourceName) { diff --git a/spring-boot-project/spring-boot/src/test/resources/config/0-empty/testproperties.properties b/spring-boot-project/spring-boot/src/test/resources/config/0-empty/testproperties.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-project/spring-boot/src/test/resources/config/1-first/testproperties.properties b/spring-boot-project/spring-boot/src/test/resources/config/1-first/testproperties.properties new file mode 100644 index 0000000000..1c97e4c412 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/config/1-first/testproperties.properties @@ -0,0 +1 @@ +first.property=apple \ No newline at end of file diff --git a/spring-boot-project/spring-boot/src/test/resources/config/2-second/testproperties.properties b/spring-boot-project/spring-boot/src/test/resources/config/2-second/testproperties.properties new file mode 100644 index 0000000000..a568eac54e --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/config/2-second/testproperties.properties @@ -0,0 +1 @@ +second.property=ball \ No newline at end of file diff --git a/spring-boot-project/spring-boot/src/test/resources/config/a/testproperties.properties b/spring-boot-project/spring-boot/src/test/resources/config/a/testproperties.properties deleted file mode 100644 index 12816704d3..0000000000 --- a/spring-boot-project/spring-boot/src/test/resources/config/a/testproperties.properties +++ /dev/null @@ -1 +0,0 @@ -a.property=apple \ No newline at end of file diff --git a/spring-boot-project/spring-boot/src/test/resources/config/b/testproperties.properties b/spring-boot-project/spring-boot/src/test/resources/config/b/testproperties.properties deleted file mode 100644 index 2db7c62843..0000000000 --- a/spring-boot-project/spring-boot/src/test/resources/config/b/testproperties.properties +++ /dev/null @@ -1 +0,0 @@ -b.property=ball \ No newline at end of file