Skip to content

Commit 76c56c6

Browse files
author
Dave Syer
committed
Add placeholder resolution to @propertysource processing
Previously the core Spring processing of @propertysource would resolve placeholders in the location attribute, but the pre-loading of the property source by Spring Boot didn't do that. Now implemented using Environment.resolvePlaceholders() (N.B. at a time when the only Environment entries available are system properties and OS env vars). E.g. @configuration @propertysource("classpath:/${source.location}.properties") protected static class WithPropertySourcePlaceholders { ... }
1 parent ca7201b commit 76c56c6

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,9 @@ public LoadCandidates(ConfigurableEnvironment environment,
360360
candidates.add(LOCATION_VARIABLE);
361361
// @PropertySource annotation locations go last here (eventually highest
362362
// priority). This unfortunately isn't the same semantics as @PropertySource
363-
// in
364-
// Spring and it's hard to change that (so the property source gets added
365-
// again in
366-
// last position by Spring later in the cycle).
367-
addLoadCandidatesFromAnnotations(resourceLoader, candidates);
363+
// in Spring and it's hard to change that (so the property source gets added
364+
// again in last position by Spring later in the cycle).
365+
addLoadCandidatesFromAnnotations(environment, resourceLoader, candidates);
368366
this.candidates = new ArrayList<String>(candidates);
369367
Collections.reverse(this.candidates);
370368
}
@@ -382,11 +380,13 @@ private void addLoadCandidatesFromSearchLocations(
382380
}
383381
}
384382

385-
private void addLoadCandidatesFromAnnotations(ResourceLoader resourceLoader,
383+
private void addLoadCandidatesFromAnnotations(
384+
ConfigurableEnvironment environment, ResourceLoader resourceLoader,
386385
Set<String> candidates) {
387386
for (String location : ConfigFileApplicationListener.this.annotations
388387
.getLocations()) {
389-
Resource resource = resourceLoader.getResource(location);
388+
Resource resource = resourceLoader.getResource(environment
389+
.resolvePlaceholders(location));
390390
if (!ConfigFileApplicationListener.this.annotations
391391
.ignoreResourceNotFound(location) && !resource.exists()) {
392392
throw new IllegalStateException("Resource not found: " + location);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ public void propertySourceAnnotation() throws Exception {
272272
context.close();
273273
}
274274

275+
@Test
276+
public void propertySourceAnnotationWithPlaceholder() throws Exception {
277+
EnvironmentTestUtils.addEnvironment(this.environment,
278+
"source.location:specificlocation");
279+
SpringApplication application = new SpringApplication(
280+
WithPropertySourcePlaceholders.class);
281+
application.setEnvironment(this.environment);
282+
application.setWebEnvironment(false);
283+
ConfigurableApplicationContext context = application.run();
284+
String property = context.getEnvironment().getProperty("my.property");
285+
assertThat(property, equalTo("fromspecificlocation"));
286+
assertNotNull(context.getEnvironment().getPropertySources()
287+
.get("classpath:/specificlocation.properties"));
288+
context.close();
289+
}
290+
275291
@Test
276292
public void propertySourceAnnotationWithName() throws Exception {
277293
SpringApplication application = new SpringApplication(
@@ -376,6 +392,12 @@ protected static class WithPropertySource {
376392

377393
}
378394

395+
@Configuration
396+
@PropertySource("classpath:/${source.location}.properties")
397+
protected static class WithPropertySourcePlaceholders {
398+
399+
}
400+
379401
@Configuration
380402
@PropertySource(value = "classpath:/specificlocation.properties", name = "foo")
381403
protected static class WithPropertySourceAndName {

0 commit comments

Comments
 (0)