diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java index da54826bd1..6e740c48b1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java @@ -16,9 +16,11 @@ package org.springframework.boot.context.config; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Deque; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -171,11 +173,22 @@ public class StandardConfigDataLocationResolver String directory, String profile) { Set references = new LinkedHashSet<>(); for (String name : this.configNames) { - for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) { - for (String extension : propertySourceLoader.getFileExtensions()) { - StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, - directory, directory + name, profile, extension, propertySourceLoader); - references.add(reference); + Deque referencesForName = getReferencesForConfigName(name, configDataLocation, + directory, profile); + references.addAll(referencesForName); + } + return references; + } + + private Deque getReferencesForConfigName(String name, + ConfigDataLocation configDataLocation, String directory, String profile) { + Deque references = new ArrayDeque<>(); + for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) { + for (String extension : propertySourceLoader.getFileExtensions()) { + StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, directory, + directory + name, profile, extension, propertySourceLoader); + if (!references.contains(reference)) { + references.addFirst(reference); } } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 479fb55673..1d8f3a5419 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -135,6 +135,13 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { assertThat(property).isEqualTo("frompropertiesfile"); } + @Test + void runWhenPropertiesAndYamlShouldPreferProperties() { + ConfigurableApplicationContext context = this.application.run(); + String property = context.getEnvironment().getProperty("duplicate"); + assertThat(property).isEqualTo("properties"); + } + @Test void runWhenMultipleCustomNamesLoadsEachName() { ConfigurableApplicationContext context = this.application diff --git a/spring-boot-project/spring-boot/src/test/resources/application.properties b/spring-boot-project/spring-boot/src/test/resources/application.properties index 2f0172e0f2..2325ace120 100644 --- a/spring-boot-project/spring-boot/src/test/resources/application.properties +++ b/spring-boot-project/spring-boot/src/test/resources/application.properties @@ -3,3 +3,4 @@ value: 1234 my.property: fromapplicationproperties sample.app.test.prop: * my.placeholder: ${my.fallback} +duplicate=properties diff --git a/spring-boot-project/spring-boot/src/test/resources/application.yml b/spring-boot-project/spring-boot/src/test/resources/application.yml new file mode 100644 index 0000000000..74fe76ca7c --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application.yml @@ -0,0 +1 @@ +duplicate: yaml \ No newline at end of file