Skip to content

Commit 32e1289

Browse files
wilkinsonaphilwebb
authored andcommitted
Use unique names for wildcard property sources
Update `StandardConfigDataLoader` to use unique names for property sources imported from a wildcard location. Prior to this commit, all the property sources created from the same wildcard location would have the same name. Each time a property source that is equal to an existing property source is added, it replaces the existing property source. Property source equality is name-based so this resulted in the last property sources from the wildcard location winning. This commit updates `StandardConfigDataLoader` to use the resolved Resource rather than the wildcard location in which it was discovered in the name of the property source that it creates, ensuring that each is property source from a wildcard location is uniquely named. Fixes gh-24428
1 parent aaaed07 commit 32e1289

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResour
4040
StandardConfigDataReference reference = resource.getReference();
4141
Resource originTrackedResource = OriginTrackedResource.of(resource.getResource(),
4242
Origin.from(reference.getConfigDataLocation()));
43-
String name = String.format("Config resource '%s' via location '%s'", reference.getResourceLocation(),
43+
String name = String.format("Config resource '%s' via location '%s'", resource,
4444
reference.getConfigDataLocation());
4545
List<PropertySource<?>> propertySources = reference.getPropertySourceLoader().load(name, originTrackedResource);
4646
return new ConfigData(propertySources);

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ void runWhenHasActiveProfileConfigurationInMultiDocumentFileLoadsInExpectedOrder
383383
List<String> names = StreamSupport.stream(context.getEnvironment().getPropertySources().spliterator(), false)
384384
.map(org.springframework.core.env.PropertySource::getName).collect(Collectors.toList());
385385
assertThat(names).contains(
386-
"Config resource 'classpath:configdata/profiles/testsetprofiles.yml' via location 'classpath:configdata/profiles/' (document #0)",
387-
"Config resource 'classpath:configdata/profiles/testsetprofiles.yml' via location 'classpath:configdata/profiles/' (document #1)");
386+
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #0)",
387+
"Config resource 'class path resource [configdata/profiles/testsetprofiles.yml]' via location 'classpath:configdata/profiles/' (document #1)");
388388
}
389389

390390
@Test
@@ -411,7 +411,7 @@ void loadWhenHasConfigLocationAsFile() {
411411
String location = "file:src/test/resources/specificlocation.properties";
412412
ConfigurableApplicationContext context = this.application.run("--spring.config.location=" + location);
413413
assertThat(context.getEnvironment()).has(matchingPropertySource(
414-
"Config resource 'file:src/test/resources/specificlocation.properties' via location '" + location
414+
"Config resource 'file [src/test/resources/specificlocation.properties]' via location '" + location
415415
+ "'"));
416416
}
417417

@@ -420,7 +420,8 @@ void loadWhenHasRelativeConfigLocationUsesFileLocation() {
420420
String location = "src/test/resources/specificlocation.properties";
421421
ConfigurableApplicationContext context = this.application.run("--spring.config.location=" + location);
422422
assertThat(context.getEnvironment()).has(matchingPropertySource(
423-
"Config resource 'src/test/resources/specificlocation.properties' via location '" + location + "'"));
423+
"Config resource 'file [src/test/resources/specificlocation.properties]' via location '" + location
424+
+ "'"));
424425
}
425426

426427
@Test
@@ -619,6 +620,16 @@ public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, Bind
619620
assertThat(origin.getParent().toString()).contains("application-import-with-placeholder");
620621
}
621622

623+
@Test
624+
void runWhenHasWildcardLocationLoadsFromAllMatchingLocations() {
625+
ConfigurableApplicationContext context = this.application.run(
626+
"--spring.config.location=optional:file:src/test/resources/config/*/",
627+
"--spring.config.name=testproperties");
628+
ConfigurableEnvironment environment = context.getEnvironment();
629+
assertThat(environment.getProperty("first.property")).isEqualTo("apple");
630+
assertThat(environment.getProperty("second.property")).isEqualTo("ball");
631+
}
632+
622633
private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {
623634
return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) {
624635

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ void loadWhenLocationResultsInMultiplePropertySourcesAddsAllToConfigData() throw
5151
assertThat(configData.getPropertySources().size()).isEqualTo(2);
5252
PropertySource<?> source1 = configData.getPropertySources().get(0);
5353
PropertySource<?> source2 = configData.getPropertySources().get(1);
54-
assertThat(source1.getName()).isEqualTo("Config resource 'classpath:configdata/yaml/application.yml' "
55-
+ "via location 'classpath:configdata/yaml/application.yml' (document #0)");
54+
assertThat(source1.getName())
55+
.isEqualTo("Config resource 'class path resource [configdata/yaml/application.yml]' "
56+
+ "via location 'classpath:configdata/yaml/application.yml' (document #0)");
5657
assertThat(source1.getProperty("foo")).isEqualTo("bar");
57-
assertThat(source2.getName()).isEqualTo("Config resource 'classpath:configdata/yaml/application.yml' "
58-
+ "via location 'classpath:configdata/yaml/application.yml' (document #1)");
58+
assertThat(source2.getName())
59+
.isEqualTo("Config resource 'class path resource [configdata/yaml/application.yml]' "
60+
+ "via location 'classpath:configdata/yaml/application.yml' (document #1)");
5961
assertThat(source2.getProperty("hello")).isEqualTo("world");
6062
}
6163

0 commit comments

Comments
 (0)