From 76c56c6aa97b028f5dc04f4b258c992622374b4d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 11 Feb 2014 14:46:00 +0000 Subject: [PATCH] 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 { ... } --- .../config/ConfigFileApplicationListener.java | 14 ++++++------ .../ConfigFileApplicationListenerTests.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) 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 c8ab2e1978f..14b619f374d 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 45e1cc20a98..5658dd2e621 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 {