diff --git a/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java index c8ab2e1978..14b619f374 100644 --- a/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java @@ -360,11 +360,9 @@ public class ConfigFileApplicationListener implements candidates.add(LOCATION_VARIABLE); // @PropertySource annotation locations go last here (eventually highest // priority). This unfortunately isn't the same semantics as @PropertySource - // in - // Spring and it's hard to change that (so the property source gets added - // again in - // last position by Spring later in the cycle). - addLoadCandidatesFromAnnotations(resourceLoader, candidates); + // in Spring and it's hard to change that (so the property source gets added + // again in last position by Spring later in the cycle). + addLoadCandidatesFromAnnotations(environment, resourceLoader, candidates); this.candidates = new ArrayList(candidates); Collections.reverse(this.candidates); } @@ -382,11 +380,13 @@ public class ConfigFileApplicationListener implements } } - private void addLoadCandidatesFromAnnotations(ResourceLoader resourceLoader, + private void addLoadCandidatesFromAnnotations( + ConfigurableEnvironment environment, ResourceLoader resourceLoader, Set candidates) { for (String location : ConfigFileApplicationListener.this.annotations .getLocations()) { - Resource resource = resourceLoader.getResource(location); + Resource resource = resourceLoader.getResource(environment + .resolvePlaceholders(location)); if (!ConfigFileApplicationListener.this.annotations .ignoreResourceNotFound(location) && !resource.exists()) { throw new IllegalStateException("Resource not found: " + location); diff --git a/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java index 45e1cc20a9..5658dd2e62 100644 --- a/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java @@ -272,6 +272,22 @@ public class ConfigFileApplicationListenerTests { context.close(); } + @Test + public void propertySourceAnnotationWithPlaceholder() throws Exception { + EnvironmentTestUtils.addEnvironment(this.environment, + "source.location:specificlocation"); + SpringApplication application = new SpringApplication( + WithPropertySourcePlaceholders.class); + application.setEnvironment(this.environment); + application.setWebEnvironment(false); + ConfigurableApplicationContext context = application.run(); + String property = context.getEnvironment().getProperty("my.property"); + assertThat(property, equalTo("fromspecificlocation")); + assertNotNull(context.getEnvironment().getPropertySources() + .get("classpath:/specificlocation.properties")); + context.close(); + } + @Test public void propertySourceAnnotationWithName() throws Exception { SpringApplication application = new SpringApplication( @@ -376,6 +392,12 @@ public class ConfigFileApplicationListenerTests { } + @Configuration + @PropertySource("classpath:/${source.location}.properties") + protected static class WithPropertySourcePlaceholders { + + } + @Configuration @PropertySource(value = "classpath:/specificlocation.properties", name = "foo") protected static class WithPropertySourceAndName {