Skip to content

Commit ceff47a

Browse files
committed
Fix ordering of properties and yaml files
Fixes gh-24719
1 parent 9a48423 commit ceff47a

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package org.springframework.boot.context.config;
1818

19+
import java.util.ArrayDeque;
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.Collections;
23+
import java.util.Deque;
2224
import java.util.LinkedHashSet;
2325
import java.util.List;
2426
import java.util.Set;
@@ -171,11 +173,22 @@ private Set<StandardConfigDataReference> getReferencesForDirectory(ConfigDataLoc
171173
String directory, String profile) {
172174
Set<StandardConfigDataReference> references = new LinkedHashSet<>();
173175
for (String name : this.configNames) {
174-
for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) {
175-
for (String extension : propertySourceLoader.getFileExtensions()) {
176-
StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation,
177-
directory, directory + name, profile, extension, propertySourceLoader);
178-
references.add(reference);
176+
Deque<StandardConfigDataReference> referencesForName = getReferencesForConfigName(name, configDataLocation,
177+
directory, profile);
178+
references.addAll(referencesForName);
179+
}
180+
return references;
181+
}
182+
183+
private Deque<StandardConfigDataReference> getReferencesForConfigName(String name,
184+
ConfigDataLocation configDataLocation, String directory, String profile) {
185+
Deque<StandardConfigDataReference> references = new ArrayDeque<>();
186+
for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) {
187+
for (String extension : propertySourceLoader.getFileExtensions()) {
188+
StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, directory,
189+
directory + name, profile, extension, propertySourceLoader);
190+
if (!references.contains(reference)) {
191+
references.addFirst(reference);
179192
}
180193
}
181194
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ void runLoadsFileWithCustomName() {
135135
assertThat(property).isEqualTo("frompropertiesfile");
136136
}
137137

138+
@Test
139+
void runWhenPropertiesAndYamlShouldPreferProperties() {
140+
ConfigurableApplicationContext context = this.application.run();
141+
String property = context.getEnvironment().getProperty("duplicate");
142+
assertThat(property).isEqualTo("properties");
143+
}
144+
138145
@Test
139146
void runWhenMultipleCustomNamesLoadsEachName() {
140147
ConfigurableApplicationContext context = this.application

spring-boot-project/spring-boot/src/test/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ value: 1234
33
my.property: fromapplicationproperties
44
sample.app.test.prop: *
55
my.placeholder: ${my.fallback}
6+
duplicate=properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
duplicate: yaml

0 commit comments

Comments
 (0)