diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 96b39121b1..b032e60bfb 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -135,7 +135,7 @@ public class ConfigFileApplicationListener implements private void onApplicationEnvironmentPreparedEvent( ConfigurableEnvironment environment, SpringApplication application) { - addPropertySources(environment); + addPropertySources(environment, application.getResourceLoader()); bindToSpringApplication(environment, application); } @@ -148,12 +148,13 @@ public class ConfigFileApplicationListener implements * @param environment the environment to add source to * @see #addPostProcessors(ConfigurableApplicationContext) */ - protected void addPropertySources(ConfigurableEnvironment environment) { + protected void addPropertySources(ConfigurableEnvironment environment, + ResourceLoader resourceLoader) { RandomValuePropertySource.addToEnvironment(environment); try { PropertySource defaultProperties = environment.getPropertySources() .remove(DEFAULT_PROPERTIES); - new Loader(environment).load(); + new Loader(environment, resourceLoader).load(); if (defaultProperties != null) { environment.getPropertySources().addLast(defaultProperties); } @@ -258,7 +259,7 @@ public class ConfigFileApplicationListener implements private final ConfigurableEnvironment environment; - private final ResourceLoader resourceLoader = new DefaultResourceLoader(); + private final ResourceLoader resourceLoader; private PropertySourcesLoader propertiesLoader; @@ -266,8 +267,10 @@ public class ConfigFileApplicationListener implements private boolean activatedProfiles; - public Loader(ConfigurableEnvironment environment) { + public Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { this.environment = environment; + this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader() + : resourceLoader; } public void load() throws IOException { diff --git a/spring-boot/src/main/java/org/springframework/boot/test/ConfigFileApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/test/ConfigFileApplicationContextInitializer.java index 593e5ec8ec..9da66bfe77 100644 --- a/spring-boot/src/main/java/org/springframework/boot/test/ConfigFileApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/test/ConfigFileApplicationContextInitializer.java @@ -36,7 +36,8 @@ public class ConfigFileApplicationContextInitializer implements public void initialize(final ConfigurableApplicationContext applicationContext) { new ConfigFileApplicationListener() { public void apply() { - addPropertySources(applicationContext.getEnvironment()); + addPropertySources(applicationContext.getEnvironment(), + applicationContext); addPostProcessors(applicationContext); } }.apply(); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 6f442bdd06..aa7cd18bbd 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -46,6 +46,9 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.env.StandardEnvironment; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -81,6 +84,34 @@ public class ConfigFileApplicationListenerTests { System.clearProperty("spring.config.location"); } + @Test + public void loadCustomResource() throws Exception { + this.event.getSpringApplication().setResourceLoader(new ResourceLoader() { + @Override + public Resource getResource(final String location) { + if (location.equals("classpath:/custom.properties")) { + return new ByteArrayResource("my.property: fromcustom".getBytes(), + location) { + @Override + public String getFilename() { + return location; + } + }; + } + return null; + } + + @Override + public ClassLoader getClassLoader() { + return getClass().getClassLoader(); + } + }); + this.initializer.setSearchNames("custom"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + assertThat(property, equalTo("fromcustom")); + } + @Test public void loadPropertiesFile() throws Exception { this.initializer.setSearchNames("testproperties");