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 da54826bd112..e9c61dd5fd6b 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,13 +173,17 @@ private Set getReferencesForDirectory(ConfigDataLoc String directory, String profile) { Set references = new LinkedHashSet<>(); for (String name : this.configNames) { + Deque referencesForName = new ArrayDeque<>(); for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) { for (String extension : propertySourceLoader.getFileExtensions()) { StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, directory, directory + name, profile, extension, propertySourceLoader); - references.add(reference); + if (!referencesForName.contains(reference)) { + referencesForName.addFirst(reference); + } } } + references.addAll(referencesForName); } return references; } 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 f9fab256442e..81f4869a0e4f 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 @@ void runLoadsFileWithCustomName() { 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 2f0172e0f2e3..2325ace12061 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 000000000000..74fe76ca7cea --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application.yml @@ -0,0 +1 @@ +duplicate: yaml \ No newline at end of file