Pass custom ResourceLoader down to read config files

If user sets a ResourceLoader on the SpringApplication, he might
reasonably expect that it would be used to read the config files.
This change ensures that it is (instead of just using the
DefaultResourceLoader).
This commit is contained in:
Dave Syer 2014-03-24 13:59:30 +00:00
parent 78d13c513c
commit 86ab2eb061
3 changed files with 41 additions and 6 deletions

View File

@ -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 {

View File

@ -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();

View File

@ -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");