Skip to content

Commit 895d092

Browse files
committed
PropertySourcesLoader with highest precedence should win
Fixes gh-13955
1 parent 1973e34 commit 895d092

File tree

6 files changed

+122
-5
lines changed

6 files changed

+122
-5
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,20 @@ private void load(String location, String name, Profile profile,
459459
if (canLoadFileExtension(loader, location)) {
460460
load(loader, location, profile,
461461
filterFactory.getDocumentFilter(profile), consumer);
462+
return;
462463
}
463464
}
464465
}
466+
Set<String> processedExtensions = new HashSet<>();
465467
for (PropertySourceLoader loader : this.propertySourceLoaders) {
466468
for (String fileExtension : loader.getFileExtensions()) {
467-
String prefix = location + name;
468-
fileExtension = "." + fileExtension;
469-
loadForFileExtension(loader, prefix, fileExtension, profile,
470-
filterFactory, consumer);
469+
if (!processedExtensions.contains(fileExtension)) {
470+
processedExtensions.add(fileExtension);
471+
String prefix = location + name;
472+
fileExtension = "." + fileExtension;
473+
loadForFileExtension(loader, prefix, fileExtension, profile,
474+
filterFactory, consumer);
475+
}
471476
}
472477
}
473478
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,21 @@ public void multiValueSpringProfiles() {
926926
assertThat(environment.getProperty("message")).isEqualTo("multiprofile");
927927
}
928928

929+
@Test
930+
public void propertiesFromCustomPropertySourceLoaderShouldBeUsed() {
931+
this.initializer.postProcessEnvironment(this.environment, this.application);
932+
assertThat(this.environment.getProperty("customloader1")).isEqualTo("true");
933+
}
934+
935+
@Test
936+
public void propertiesFromCustomPropertySourceLoaderShouldBeUsedWithSpecificResource() {
937+
String location = "classpath:application.custom";
938+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
939+
"spring.config.location=" + location);
940+
this.initializer.postProcessEnvironment(this.environment, this.application);
941+
assertThat(this.environment.getProperty("customloader1")).isEqualTo("true");
942+
}
943+
929944
private Condition<ConfigurableEnvironment> matchingPropertySource(
930945
final String sourceName) {
931946
return new Condition<ConfigurableEnvironment>(
@@ -1018,7 +1033,7 @@ private static class LowestPrecedenceEnvironmentPostProcessor
10181033
@Override
10191034
public void postProcessEnvironment(ConfigurableEnvironment environment,
10201035
SpringApplication application) {
1021-
assertThat(environment.getPropertySources()).hasSize(4);
1036+
assertThat(environment.getPropertySources()).hasSize(5);
10221037
}
10231038

10241039
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.context.config;
17+
18+
import java.io.IOException;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.springframework.boot.env.PropertySourceLoader;
24+
import org.springframework.core.env.MapPropertySource;
25+
import org.springframework.core.env.PropertySource;
26+
import org.springframework.core.io.Resource;
27+
28+
/**
29+
* {@link PropertySourceLoader} for tests.
30+
*
31+
* @author Madhura Bhave
32+
*/
33+
class TestPropertySourceLoader1 implements PropertySourceLoader {
34+
35+
@Override
36+
public String[] getFileExtensions() {
37+
return new String[] { "custom" };
38+
}
39+
40+
@Override
41+
public List<PropertySource<?>> load(String name, Resource resource)
42+
throws IOException {
43+
Map<String, Object> map = Collections.singletonMap("customloader1", "true");
44+
return Collections.singletonList(new MapPropertySource(name, map));
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.context.config;
17+
18+
import java.io.IOException;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import org.springframework.boot.env.PropertySourceLoader;
24+
import org.springframework.core.env.MapPropertySource;
25+
import org.springframework.core.env.PropertySource;
26+
import org.springframework.core.io.Resource;
27+
28+
/**
29+
* {@link PropertySourceLoader} for tests.
30+
*
31+
* @author Madhura Bhave
32+
*/
33+
class TestPropertySourceLoader2 implements PropertySourceLoader {
34+
35+
@Override
36+
public String[] getFileExtensions() {
37+
return new String[] { "custom" };
38+
}
39+
40+
@Override
41+
public List<PropertySource<?>> load(String name, Resource resource)
42+
throws IOException {
43+
Map<String, Object> map = Collections.singletonMap("customloader2", "true");
44+
return Collections.singletonList(new MapPropertySource(name, map));
45+
}
46+
47+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.springframework.boot.env.PropertySourceLoader=\
2+
org.springframework.boot.context.config.TestPropertySourceLoader1,\
3+
org.springframework.boot.context.config.TestPropertySourceLoader2

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

Whitespace-only changes.

0 commit comments

Comments
 (0)