Introduce defined extension point for modifying the environment

The commit introduces a new extension point, EnvironmentPostProcessor,
that can be implemented by classes that want to modify the
environment. Implementations of EnvironmentPostProcessor are loaded
via spring.factories and called in response to the
ApplicationEnvironmentPreparedEvent. Application listeners that wish
to work with the post-processed environment can continue to listen
to ApplicationEnvironmentPreparedEvent and order themselves to
run after EnvironmentPostProcessingApplicationListener.

Existing ApplicationListeners that modify the environment have,
where possible, been updated to implement EnvironmentPostProcessor
instead.

Closes gh-3737
This commit is contained in:
Andy Wilkinson 2015-08-13 10:42:39 +01:00
parent 2f862cfb92
commit e370b592d6
11 changed files with 204 additions and 128 deletions

View File

@ -455,7 +455,7 @@ public class SpringApplication {
* @param environment this application's environment * @param environment this application's environment
* @param args arguments passed to the {@code run} method * @param args arguments passed to the {@code run} method
* @see #configureEnvironment(ConfigurableEnvironment, String[]) * @see #configureEnvironment(ConfigurableEnvironment, String[])
* @see org.springframework.boot.context.config.ConfigFileApplicationListener * @see org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor
*/ */
protected void configureProfiles(ConfigurableEnvironment environment, String[] args) { protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
environment.getActiveProfiles(); // ensure they are initialized environment.getActiveProfiles(); // ensure they are initialized

View File

@ -25,11 +25,11 @@ import java.util.Properties;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.config.ConfigFileApplicationListener; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.json.JsonParser; import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory; import org.springframework.boot.json.JsonParserFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
@ -39,8 +39,8 @@ import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* An {@link ApplicationListener} that knows where to find VCAP (a.k.a. Cloud Foundry) * An {@link EnvironmentPostProcessor} that knows where to find VCAP (a.k.a. Cloud
* meta data in the existing environment. It parses out the VCAP_APPLICATION and * Foundry) meta data in the existing environment. It parses out the VCAP_APPLICATION and
* VCAP_SERVICES meta data and dumps it in a form that is easily consumed by * VCAP_SERVICES meta data and dumps it in a form that is easily consumed by
* {@link Environment} users. If the app is running in Cloud Foundry then both meta data * {@link Environment} users. If the app is running in Cloud Foundry then both meta data
* items are JSON objects encoded in OS environment variables. VCAP_APPLICATION is a * items are JSON objects encoded in OS environment variables. VCAP_APPLICATION is a
@ -86,18 +86,19 @@ import org.springframework.util.StringUtils;
* Cloud is more convenient and more robust against potential changes in Cloud Foundry. * Cloud is more convenient and more robust against potential changes in Cloud Foundry.
* *
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
*/ */
public class VcapApplicationListener implements public class VcapEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
private static final Log logger = LogFactory.getLog(VcapApplicationListener.class); private static final Log logger = LogFactory
.getLog(VcapEnvironmentPostProcessor.class);
private static final String VCAP_APPLICATION = "VCAP_APPLICATION"; private static final String VCAP_APPLICATION = "VCAP_APPLICATION";
private static final String VCAP_SERVICES = "VCAP_SERVICES"; private static final String VCAP_SERVICES = "VCAP_SERVICES";
// Before ConfigFileApplicationListener so values there can use these ones // Before ConfigFileApplicationListener so values there can use these ones
private int order = ConfigFileApplicationListener.DEFAULT_ORDER - 1; private int order = ConfigFileEnvironmentPostProcessor.DEFAULT_ORDER - 1;
private final JsonParser parser = JsonParserFactory.getJsonParser(); private final JsonParser parser = JsonParserFactory.getJsonParser();
@ -111,8 +112,8 @@ public class VcapApplicationListener implements
} }
@Override @Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { public void postProcessEnvironment(ConfigurableEnvironment environment,
ConfigurableEnvironment environment = event.getEnvironment(); SpringApplication application) {
if (!environment.containsProperty(VCAP_APPLICATION) if (!environment.containsProperty(VCAP_APPLICATION)
&& !environment.containsProperty(VCAP_SERVICES)) { && !environment.containsProperty(VCAP_SERVICES)) {
return; return;

View File

@ -20,6 +20,7 @@ import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled; import org.springframework.boot.ansi.AnsiOutput.Enabled;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.env.EnvironmentPostProcessingApplicationListener;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
@ -51,8 +52,9 @@ public class AnsiOutputApplicationListener implements
@Override @Override
public int getOrder() { public int getOrder() {
// Apply after the ConfigFileApplicationListener // Apply after the EnvironmentPostProcessingApplicationListener has called all
return ConfigFileApplicationListener.DEFAULT_ORDER + 1; // EnvironmentPostProcessors
return EnvironmentPostProcessingApplicationListener.ORDER + 1;
} }
} }

View File

@ -33,12 +33,11 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.PropertiesConfigurationFactory; import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnumerableCompositePropertySource; import org.springframework.boot.env.EnumerableCompositePropertySource;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.PropertySourcesLoader; import org.springframework.boot.env.PropertySourcesLoader;
import org.springframework.boot.logging.DeferredLog; import org.springframework.boot.logging.DeferredLog;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ConfigurationClassPostProcessor; import org.springframework.context.annotation.ConfigurationClassPostProcessor;
@ -47,7 +46,6 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
@ -59,7 +57,7 @@ import org.springframework.util.StringUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
/** /**
* {@link ApplicationListener} that configures the context environment by loading * {@link EnvironmentPostProcessor} that configures the context environment by loading
* properties from well known file locations. By default properties will be loaded from * properties from well known file locations. By default properties will be loaded from
* 'application.properties' and/or 'application.yml' files in the following locations: * 'application.properties' and/or 'application.yml' files in the following locations:
* <ul> * <ul>
@ -89,9 +87,10 @@ import org.springframework.validation.BindException;
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson
*/ */
public class ConfigFileApplicationListener implements public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProcessor,
ApplicationListener<ApplicationEvent>, Ordered { ApplicationListener<ApplicationPreparedEvent>, Ordered {
private static final String DEFAULT_PROPERTIES = "defaultProperties"; private static final String DEFAULT_PROPERTIES = "defaultProperties";
@ -121,34 +120,15 @@ public class ConfigFileApplicationListener implements
private final ConversionService conversionService = new DefaultConversionService(); private final ConversionService conversionService = new DefaultConversionService();
@Override @Override
public void onApplicationEvent(ApplicationEvent event) { public void postProcessEnvironment(ConfigurableEnvironment environment,
if (event instanceof ApplicationEnvironmentPreparedEvent) { SpringApplication application) {
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
}
}
private void onApplicationEnvironmentPreparedEvent(
ApplicationEnvironmentPreparedEvent event) {
Environment environment = event.getEnvironment();
if (environment instanceof ConfigurableEnvironment) {
onApplicationEnvironmentPreparedEvent((ConfigurableEnvironment) environment,
event.getSpringApplication());
}
}
private void onApplicationEnvironmentPreparedEvent(
ConfigurableEnvironment environment, SpringApplication application) {
addPropertySources(environment, application.getResourceLoader()); addPropertySources(environment, application.getResourceLoader());
bindToSpringApplication(environment, application); bindToSpringApplication(environment, application);
} }
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) { @Override
// logging is deferred because the Logging initialization might not have public void onApplicationEvent(ApplicationPreparedEvent event) {
// run at the time that config file decisions are taken this.logger.replayTo(ConfigFileEnvironmentPostProcessor.class);
this.logger.replayTo(ConfigFileApplicationListener.class);
addPostProcessors(event.getApplicationContext()); addPostProcessors(event.getApplicationContext());
} }
@ -272,7 +252,7 @@ public class ConfigFileApplicationListener implements
*/ */
private class Loader { private class Loader {
private final Log logger = ConfigFileApplicationListener.this.logger; private final Log logger = ConfigFileEnvironmentPostProcessor.this.logger;
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
@ -472,7 +452,7 @@ public class ConfigFileApplicationListener implements
} }
} }
locations.addAll(asResolvedSet( locations.addAll(asResolvedSet(
ConfigFileApplicationListener.this.searchLocations, ConfigFileEnvironmentPostProcessor.this.searchLocations,
DEFAULT_SEARCH_LOCATIONS)); DEFAULT_SEARCH_LOCATIONS));
return locations; return locations;
} }
@ -482,7 +462,8 @@ public class ConfigFileApplicationListener implements
return asResolvedSet(this.environment.getProperty(CONFIG_NAME_PROPERTY), return asResolvedSet(this.environment.getProperty(CONFIG_NAME_PROPERTY),
null); null);
} }
return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES); return asResolvedSet(ConfigFileEnvironmentPostProcessor.this.names,
DEFAULT_NAMES);
} }
private Set<String> asResolvedSet(String value, String fallback) { private Set<String> asResolvedSet(String value, String fallback) {

View File

@ -18,7 +18,7 @@
* External configuration support allowing 'application.properties' to be loaded * External configuration support allowing 'application.properties' to be loaded
* and used within a Spring Boot application. * and used within a Spring Boot application.
* *
* @see org.springframework.boot.context.config.ConfigFileApplicationListener * @see org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor
*/ */
package org.springframework.boot.context.config; package org.springframework.boot.context.config;

View File

@ -0,0 +1,61 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.env;
import java.util.List;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.io.support.SpringFactoriesLoader;
/**
* An {@link ApplicationListener} that responds to an
* {@link ApplicationEnvironmentPreparedEvent} and calls all
* {@link EnvironmentPostProcessor EnvironmentPostProcessors} that are available via
* {@code spring.factories}.
* <p>
* Post-processors are called in the order defined by
* {@link AnnotationAwareOrderComparator}.
*
* @author Andy Wilkinson
* @since 1.3.0
* @see SpringFactoriesLoader#loadFactories(Class, ClassLoader)
*/
public class EnvironmentPostProcessingApplicationListener implements
ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
public static final int ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
List<EnvironmentPostProcessor> postProcessors = SpringFactoriesLoader
.loadFactories(EnvironmentPostProcessor.class, getClass()
.getClassLoader());
for (EnvironmentPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessEnvironment(event.getEnvironment(),
event.getSpringApplication());
}
}
@Override
public int getOrder() {
return ORDER;
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.env;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
/**
* Allows for customization of the application's {@link Environment} prior to the
* application context being refreshed.
*
* @author Andy Wilkinson
* @since 1.3.0
*/
public interface EnvironmentPostProcessor {
/**
* Post-process the given {@code environment}
*
* @param environment the environment to post-process
* @param application the application to which the environment belongs
*/
void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application);
}

View File

@ -16,7 +16,7 @@
package org.springframework.boot.test; package org.springframework.boot.test;
import org.springframework.boot.context.config.ConfigFileApplicationListener; import org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -27,14 +27,14 @@ import org.springframework.test.context.ContextConfiguration;
* {@literal application.properties}. * {@literal application.properties}.
* *
* @author Phillip Webb * @author Phillip Webb
* @see ConfigFileApplicationListener * @see ConfigFileEnvironmentPostProcessor
*/ */
public class ConfigFileApplicationContextInitializer implements public class ConfigFileApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> { ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override @Override
public void initialize(final ConfigurableApplicationContext applicationContext) { public void initialize(final ConfigurableApplicationContext applicationContext) {
new ConfigFileApplicationListener() { new ConfigFileEnvironmentPostProcessor() {
public void apply() { public void apply() {
addPropertySources(applicationContext.getEnvironment(), addPropertySources(applicationContext.getEnvironment(),
applicationContext); applicationContext);

View File

@ -17,11 +17,16 @@ org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer
# Application Listeners # Application Listeners
org.springframework.context.ApplicationListener=\ org.springframework.context.ApplicationListener=\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\ org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.cloudfoundry.VcapApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\ org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\ org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\ org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor,\
org.springframework.boot.context.config.DelegatingApplicationListener,\ org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.env.EnvironmentPostProcessingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\ org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\
org.springframework.boot.logging.ClasspathLoggingApplicationListener,\ org.springframework.boot.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.logging.LoggingApplicationListener org.springframework.boot.logging.LoggingApplicationListener
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloudfoundry.VcapEnvironmentPostProcessor,\
org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor

View File

@ -17,8 +17,6 @@
package org.springframework.boot.cloudfoundry; package org.springframework.boot.cloudfoundry;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -27,19 +25,17 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
/** /**
* Tests for {@link VcapApplicationListener}. * Tests for {@link VcapEnvironmentPostProcessor}.
* *
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
*/ */
public class VcapApplicationListenerTests { public class VcapEnvironmentPostProcessorTests {
private final VcapApplicationListener initializer = new VcapApplicationListener(); private final VcapEnvironmentPostProcessor initializer = new VcapEnvironmentPostProcessor();
private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(
new SpringApplication(), new String[0], this.context.getEnvironment());
@Test @Test
public void testApplicationProperties() { public void testApplicationProperties() {
EnvironmentTestUtils EnvironmentTestUtils
@ -57,7 +53,7 @@ public class VcapApplicationListenerTests {
+ "\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"]," + "\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"],"
+ "\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\"," + "\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\","
+ "\"state_timestamp\":1369795079}"); + "\"state_timestamp\":1369795079}");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertEquals("bb7935245adf3e650dfb7c58a06e9ece", this.context.getEnvironment() assertEquals("bb7935245adf3e650dfb7c58a06e9ece", this.context.getEnvironment()
.getProperty("vcap.application.instance_id")); .getProperty("vcap.application.instance_id"));
} }
@ -68,7 +64,7 @@ public class VcapApplicationListenerTests {
.addEnvironment( .addEnvironment(
this.context, this.context,
"VCAP_APPLICATION:{\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}"); "VCAP_APPLICATION:{\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertEquals("foo.cfapps.io", assertEquals("foo.cfapps.io",
this.context.getEnvironment().getProperty("vcap.application.uris[0]")); this.context.getEnvironment().getProperty("vcap.application.uris[0]"));
} }
@ -76,7 +72,7 @@ public class VcapApplicationListenerTests {
@Test @Test
public void testUnparseableApplicationProperties() { public void testUnparseableApplicationProperties() {
EnvironmentTestUtils.addEnvironment(this.context, "VCAP_APPLICATION:"); EnvironmentTestUtils.addEnvironment(this.context, "VCAP_APPLICATION:");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertNull(getProperty("vcap")); assertNull(getProperty("vcap"));
} }
@ -97,7 +93,7 @@ public class VcapApplicationListenerTests {
+ "\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"]," + "\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"],"
+ "\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\"," + "\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\","
+ "\"state_timestamp\":1369795079}"); + "\"state_timestamp\":1369795079}");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertNull(getProperty("vcap")); assertNull(getProperty("vcap"));
} }
@ -115,7 +111,7 @@ public class VcapApplicationListenerTests {
+ "\"host\":\"mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\"," + "\"host\":\"mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\","
+ "\"port\":3306,\"user\":\"urpRuqTf8Cpe6\",\"username\":" + "\"port\":3306,\"user\":\"urpRuqTf8Cpe6\",\"username\":"
+ "\"urpRuqTf8Cpe6\",\"password\":\"pxLsGVpsC9A5S\"}}]}"); + "\"urpRuqTf8Cpe6\",\"password\":\"pxLsGVpsC9A5S\"}}]}");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertEquals("mysql", getProperty("vcap.services.mysql.name")); assertEquals("mysql", getProperty("vcap.services.mysql.name"));
assertEquals("3306", getProperty("vcap.services.mysql.credentials.port")); assertEquals("3306", getProperty("vcap.services.mysql.credentials.port"));
assertEquals("true", getProperty("vcap.services.mysql.credentials.ssl")); assertEquals("true", getProperty("vcap.services.mysql.credentials.ssl"));
@ -135,7 +131,7 @@ public class VcapApplicationListenerTests {
+ "\"port\":3306,\"user\":\"urpRuqTf8Cpe6\"," + "\"port\":3306,\"user\":\"urpRuqTf8Cpe6\","
+ "\"username\":\"urpRuqTf8Cpe6\"," + "\"username\":\"urpRuqTf8Cpe6\","
+ "\"password\":\"pxLsGVpsC9A5S\"}}]}"); + "\"password\":\"pxLsGVpsC9A5S\"}}]}");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.context.getEnvironment(), null);
assertEquals("mysql", getProperty("vcap.services.mysql.name")); assertEquals("mysql", getProperty("vcap.services.mysql.name"));
assertEquals("3306", getProperty("vcap.services.mysql.credentials.port")); assertEquals("3306", getProperty("vcap.services.mysql.credentials.port"));
} }

View File

@ -35,8 +35,7 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener.ConfigurationPropertySources; import org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor.ConfigurationPropertySources;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.env.EnumerableCompositePropertySource; import org.springframework.boot.env.EnumerableCompositePropertySource;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -63,19 +62,18 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link ConfigFileApplicationListener}. * Tests for {@link ConfigFileEnvironmentPostProcessor}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer * @author Dave Syer
*/ */
public class ConfigFileApplicationListenerTests { public class ConfigFileEnvironmentPostProcessorTests {
private final StandardEnvironment environment = new StandardEnvironment(); private final StandardEnvironment environment = new StandardEnvironment();
private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent( private final SpringApplication application = new SpringApplication();
new SpringApplication(), new String[0], this.environment);
private final ConfigFileApplicationListener initializer = new ConfigFileApplicationListener(); private final ConfigFileEnvironmentPostProcessor initializer = new ConfigFileEnvironmentPostProcessor();
@Rule @Rule
public ExpectedException expected = ExpectedException.none(); public ExpectedException expected = ExpectedException.none();
@ -89,7 +87,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadCustomResource() throws Exception { public void loadCustomResource() throws Exception {
this.event.getSpringApplication().setResourceLoader(new ResourceLoader() { this.application.setResourceLoader(new ResourceLoader() {
@Override @Override
public Resource getResource(final String location) { public Resource getResource(final String location) {
if (location.equals("classpath:/custom.properties")) { if (location.equals("classpath:/custom.properties")) {
@ -110,7 +108,7 @@ public class ConfigFileApplicationListenerTests {
} }
}); });
this.initializer.setSearchNames("custom"); this.initializer.setSearchNames("custom");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("fromcustom")); assertThat(property, equalTo("fromcustom"));
} }
@ -118,7 +116,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadPropertiesFile() throws Exception { public void loadPropertiesFile() throws Exception {
this.initializer.setSearchNames("testproperties"); this.initializer.setSearchNames("testproperties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("frompropertiesfile")); assertThat(property, equalTo("frompropertiesfile"));
} }
@ -127,7 +125,7 @@ public class ConfigFileApplicationListenerTests {
public void loadDefaultPropertiesFile() throws Exception { public void loadDefaultPropertiesFile() throws Exception {
this.environment.setDefaultProfiles("thedefault"); this.environment.setDefaultProfiles("thedefault");
this.initializer.setSearchNames("testprofiles"); this.initializer.setSearchNames("testprofiles");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("fromdefaultpropertiesfile")); assertThat(property, equalTo("fromdefaultpropertiesfile"));
} }
@ -136,7 +134,7 @@ public class ConfigFileApplicationListenerTests {
public void loadTwoPropertiesFile() throws Exception { public void loadTwoPropertiesFile() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ "classpath:application.properties,classpath:testproperties.properties"); + "classpath:application.properties,classpath:testproperties.properties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("frompropertiesfile")); assertThat(property, equalTo("frompropertiesfile"));
} }
@ -145,7 +143,7 @@ public class ConfigFileApplicationListenerTests {
public void loadTwoPropertiesFilesWithProfiles() throws Exception { public void loadTwoPropertiesFilesWithProfiles() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ "classpath:enableprofile.properties,classpath:enableother.properties"); + "classpath:enableprofile.properties,classpath:enableother.properties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertEquals("other", StringUtils.arrayToCommaDelimitedString(this.environment assertEquals("other", StringUtils.arrayToCommaDelimitedString(this.environment
.getActiveProfiles())); .getActiveProfiles()));
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
@ -157,7 +155,7 @@ public class ConfigFileApplicationListenerTests {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ "classpath:enabletwoprofiles.properties," + "classpath:enabletwoprofiles.properties,"
+ "classpath:enableprofile.properties"); + "classpath:enableprofile.properties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertEquals("myprofile", assertEquals("myprofile",
StringUtils.arrayToCommaDelimitedString(this.environment StringUtils.arrayToCommaDelimitedString(this.environment
.getActiveProfiles())); .getActiveProfiles()));
@ -173,7 +171,7 @@ public class ConfigFileApplicationListenerTests {
EnvironmentTestUtils.addEnvironment(this.environment, EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.name:enabletwoprofiles", "spring.config.name:enabletwoprofiles",
"spring.config.location:classpath:enableprofile.properties"); "spring.config.location:classpath:enableprofile.properties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertEquals("myprofile", assertEquals("myprofile",
StringUtils.arrayToCommaDelimitedString(this.environment StringUtils.arrayToCommaDelimitedString(this.environment
.getActiveProfiles())); .getActiveProfiles()));
@ -197,7 +195,7 @@ public class ConfigFileApplicationListenerTests {
finally { finally {
out.close(); out.close();
} }
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("fromlocalfile")); assertThat(property, equalTo("fromlocalfile"));
} }
@ -210,7 +208,7 @@ public class ConfigFileApplicationListenerTests {
public void moreSpecificLocationTakesPrecedenceOverRoot() throws Exception { public void moreSpecificLocationTakesPrecedenceOverRoot() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, EnvironmentTestUtils.addEnvironment(this.environment,
"spring.config.name:specific"); "spring.config.name:specific");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("specific")); assertThat(property, equalTo("specific"));
} }
@ -221,14 +219,14 @@ public class ConfigFileApplicationListenerTests {
+ "classpath:application.properties," + "classpath:application.properties,"
+ "classpath:testproperties.properties," + "classpath:testproperties.properties,"
+ "classpath:nonexistent.properties"); + "classpath:nonexistent.properties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("frompropertiesfile")); assertThat(property, equalTo("frompropertiesfile"));
} }
@Test @Test
public void randomValue() throws Exception { public void randomValue() throws Exception {
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("random.value"); String property = this.environment.getProperty("random.value");
assertThat(property, notNullValue()); assertThat(property, notNullValue());
} }
@ -236,7 +234,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadTwoPropertiesFiles() throws Exception { public void loadTwoPropertiesFiles() throws Exception {
this.initializer.setSearchNames("moreproperties,testproperties"); this.initializer.setSearchNames("moreproperties,testproperties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
// The search order has highest precedence last (like merging a map) // The search order has highest precedence last (like merging a map)
assertThat(property, equalTo("frompropertiesfile")); assertThat(property, equalTo("frompropertiesfile"));
@ -245,7 +243,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadYamlFile() throws Exception { public void loadYamlFile() throws Exception {
this.initializer.setSearchNames("testyaml"); this.initializer.setSearchNames("testyaml");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromyamlfile")); assertThat(property, equalTo("fromyamlfile"));
assertThat(this.environment.getProperty("my.array[0]"), equalTo("1")); assertThat(this.environment.getProperty("my.array[0]"), equalTo("1"));
@ -255,7 +253,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadProfileEmptySameAsNotSpecified() throws Exception { public void loadProfileEmptySameAsNotSpecified() throws Exception {
this.initializer.setSearchNames("testprofilesempty"); this.initializer.setSearchNames("testprofilesempty");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromemptyprofile")); assertThat(property, equalTo("fromemptyprofile"));
} }
@ -264,7 +262,7 @@ public class ConfigFileApplicationListenerTests {
public void loadDefaultYamlDocument() throws Exception { public void loadDefaultYamlDocument() throws Exception {
this.environment.setDefaultProfiles("thedefault"); this.environment.setDefaultProfiles("thedefault");
this.initializer.setSearchNames("testprofilesdocument"); this.initializer.setSearchNames("testprofilesdocument");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromdefaultprofile")); assertThat(property, equalTo("fromdefaultprofile"));
} }
@ -274,7 +272,7 @@ public class ConfigFileApplicationListenerTests {
this.environment.setDefaultProfiles("thedefault"); this.environment.setDefaultProfiles("thedefault");
this.environment.setActiveProfiles("other"); this.environment.setActiveProfiles("other");
this.initializer.setSearchNames("testprofilesdocument"); this.initializer.setSearchNames("testprofilesdocument");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromotherprofile")); assertThat(property, equalTo("fromotherprofile"));
} }
@ -284,7 +282,7 @@ public class ConfigFileApplicationListenerTests {
this.environment.getPropertySources().addFirst( this.environment.getPropertySources().addFirst(
new SimpleCommandLinePropertySource("--the.property=fromcommandline")); new SimpleCommandLinePropertySource("--the.property=fromcommandline"));
this.initializer.setSearchNames("testproperties"); this.initializer.setSearchNames("testproperties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("fromcommandline")); assertThat(property, equalTo("fromcommandline"));
} }
@ -293,34 +291,27 @@ public class ConfigFileApplicationListenerTests {
public void systemPropertyWins() throws Exception { public void systemPropertyWins() throws Exception {
System.setProperty("the.property", "fromsystem"); System.setProperty("the.property", "fromsystem");
this.initializer.setSearchNames("testproperties"); this.initializer.setSearchNames("testproperties");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("fromsystem")); assertThat(property, equalTo("fromsystem"));
} }
@Test @Test
public void defaultPropertyAsFallback() throws Exception { public void defaultPropertyAsFallback() throws Exception {
this.event this.environment.getPropertySources().addLast(
.getEnvironment() new MapPropertySource("defaultProperties", Collections.singletonMap(
.getPropertySources() "my.fallback", (Object) "foo")));
.addLast( this.initializer.postProcessEnvironment(this.environment, this.application);
new MapPropertySource("defaultProperties", Collections
.singletonMap("my.fallback", (Object) "foo")));
this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.fallback"); String property = this.environment.getProperty("my.fallback");
assertThat(property, equalTo("foo")); assertThat(property, equalTo("foo"));
} }
@Test @Test
public void defaultPropertyAsFallbackDuringFileParsing() throws Exception { public void defaultPropertyAsFallbackDuringFileParsing() throws Exception {
this.event this.environment.getPropertySources().addLast(
.getEnvironment() new MapPropertySource("defaultProperties", Collections.singletonMap(
.getPropertySources() "spring.config.name", (Object) "testproperties")));
.addLast( this.initializer.postProcessEnvironment(this.environment, this.application);
new MapPropertySource("defaultProperties", Collections
.singletonMap("spring.config.name",
(Object) "testproperties")));
this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("frompropertiesfile")); assertThat(property, equalTo("frompropertiesfile"));
} }
@ -331,7 +322,7 @@ public class ConfigFileApplicationListenerTests {
// This should be the effect of calling // This should be the effect of calling
// SpringApplication.setAdditionalProfiles("other") // SpringApplication.setAdditionalProfiles("other")
this.environment.setActiveProfiles("other"); this.environment.setActiveProfiles("other");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
// The "other" profile is activated in SpringApplication so it should take // The "other" profile is activated in SpringApplication so it should take
// precedence over the default profile // precedence over the default profile
@ -343,7 +334,7 @@ public class ConfigFileApplicationListenerTests {
// This should be the effect of calling // This should be the effect of calling
// SpringApplication.setAdditionalProfiles("other", "dev") // SpringApplication.setAdditionalProfiles("other", "dev")
this.environment.setActiveProfiles("other", "dev"); this.environment.setActiveProfiles("other", "dev");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
// The "dev" profile is activated in SpringApplication so it should take // The "dev" profile is activated in SpringApplication so it should take
// precedence over the default profile // precedence over the default profile
@ -353,7 +344,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void loadPropertiesThenProfilePropertiesActivatedInFirst() throws Exception { public void loadPropertiesThenProfilePropertiesActivatedInFirst() throws Exception {
this.initializer.setSearchNames("enableprofile"); this.initializer.setSearchNames("enableprofile");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
// The "myprofile" profile is activated in enableprofile.properties so its value // The "myprofile" profile is activated in enableprofile.properties so its value
// should show up here // should show up here
@ -366,7 +357,7 @@ public class ConfigFileApplicationListenerTests {
// EnvironmentTestUtils.addEnvironment(this.environment, // EnvironmentTestUtils.addEnvironment(this.environment,
// "spring.profiles.active:other"); // "spring.profiles.active:other");
this.initializer.setSearchNames("enableprofile"); this.initializer.setSearchNames("enableprofile");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("other.property"); String property = this.environment.getProperty("other.property");
// The "other" profile is activated before any processing starts // The "other" profile is activated before any processing starts
assertThat(property, equalTo("fromotherpropertiesfile")); assertThat(property, equalTo("fromotherpropertiesfile"));
@ -380,7 +371,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void profilePropertiesUsedInPlaceholders() throws Exception { public void profilePropertiesUsedInPlaceholders() throws Exception {
this.initializer.setSearchNames("enableprofile"); this.initializer.setSearchNames("enableprofile");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("one.more"); String property = this.environment.getProperty("one.more");
assertThat(property, equalTo("fromprofilepropertiesfile")); assertThat(property, equalTo("fromprofilepropertiesfile"));
} }
@ -389,7 +380,7 @@ public class ConfigFileApplicationListenerTests {
public void yamlProfiles() throws Exception { public void yamlProfiles() throws Exception {
this.initializer.setSearchNames("testprofiles"); this.initializer.setSearchNames("testprofiles");
this.environment.setActiveProfiles("dev"); this.environment.setActiveProfiles("dev");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromdevprofile")); assertThat(property, equalTo("fromdevprofile"));
property = this.environment.getProperty("my.other"); property = this.environment.getProperty("my.other");
@ -400,7 +391,7 @@ public class ConfigFileApplicationListenerTests {
public void yamlTwoProfiles() throws Exception { public void yamlTwoProfiles() throws Exception {
this.initializer.setSearchNames("testprofiles"); this.initializer.setSearchNames("testprofiles");
this.environment.setActiveProfiles("other", "dev"); this.environment.setActiveProfiles("other", "dev");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromdevprofile")); assertThat(property, equalTo("fromdevprofile"));
property = this.environment.getProperty("my.other"); property = this.environment.getProperty("my.other");
@ -410,7 +401,7 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void yamlSetsProfiles() throws Exception { public void yamlSetsProfiles() throws Exception {
this.initializer.setSearchNames("testsetprofiles"); this.initializer.setSearchNames("testsetprofiles");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertEquals("dev", StringUtils.arrayToCommaDelimitedString(this.environment assertEquals("dev", StringUtils.arrayToCommaDelimitedString(this.environment
.getActiveProfiles())); .getActiveProfiles()));
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
@ -444,7 +435,7 @@ public class ConfigFileApplicationListenerTests {
EnvironmentTestUtils.addEnvironment(this.environment, EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active:prod"); "spring.profiles.active:prod");
this.initializer.setSearchNames("testsetprofiles"); this.initializer.setSearchNames("testsetprofiles");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles(), equalTo(new String[] { "prod" })); assertThat(this.environment.getActiveProfiles(), equalTo(new String[] { "prod" }));
} }
@ -453,7 +444,7 @@ public class ConfigFileApplicationListenerTests {
EnvironmentTestUtils.addEnvironment(this.environment, EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active=specificprofile", "spring.profiles.active=specificprofile",
"spring.config.name=specificfile"); "spring.config.name=specificfile");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromspecificpropertiesfile")); assertThat(property, equalTo("fromspecificpropertiesfile"));
} }
@ -463,7 +454,7 @@ public class ConfigFileApplicationListenerTests {
String location = "classpath:specificlocation.properties"; String location = "classpath:specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ location); + location);
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property"); String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("fromspecificlocation")); assertThat(property, equalTo("fromspecificlocation"));
assertThat(this.environment, containsPropertySource("applicationConfig: " assertThat(this.environment, containsPropertySource("applicationConfig: "
@ -479,7 +470,7 @@ public class ConfigFileApplicationListenerTests {
String location = "file:src/test/resources/specificlocation.properties"; String location = "file:src/test/resources/specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ location); + location);
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment, containsPropertySource("applicationConfig: [" assertThat(this.environment, containsPropertySource("applicationConfig: ["
+ location + "]")); + location + "]"));
} }
@ -489,7 +480,7 @@ public class ConfigFileApplicationListenerTests {
String location = "src/test/resources/specificlocation.properties"; String location = "src/test/resources/specificlocation.properties";
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ location); + location);
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment, containsPropertySource("applicationConfig: [file:" assertThat(this.environment, containsPropertySource("applicationConfig: [file:"
+ location + "]")); + location + "]"));
} }
@ -500,7 +491,7 @@ public class ConfigFileApplicationListenerTests {
.getAbsolutePath(); .getAbsolutePath();
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
+ location); + location);
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment, containsPropertySource("applicationConfig: [file:" assertThat(this.environment, containsPropertySource("applicationConfig: [file:"
+ location.replace(File.separatorChar, '/') + "]")); + location.replace(File.separatorChar, '/') + "]"));
} }
@ -633,22 +624,20 @@ public class ConfigFileApplicationListenerTests {
public void bindsToSpringApplication() throws Exception { public void bindsToSpringApplication() throws Exception {
// gh-346 // gh-346
this.initializer.setSearchNames("bindtoapplication"); this.initializer.setSearchNames("bindtoapplication");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
SpringApplication application = this.event.getSpringApplication();
Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner"); Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner");
field.setAccessible(true); field.setAccessible(true);
assertThat((Boolean) field.get(application), equalTo(false)); assertThat((Boolean) field.get(this.application), equalTo(false));
} }
@Test @Test
public void bindsSystemPropertyToSpringApplication() throws Exception { public void bindsSystemPropertyToSpringApplication() throws Exception {
// gh-951 // gh-951
System.setProperty("spring.main.showBanner", "false"); System.setProperty("spring.main.showBanner", "false");
this.initializer.onApplicationEvent(this.event); this.initializer.postProcessEnvironment(this.environment, this.application);
SpringApplication application = this.event.getSpringApplication();
Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner"); Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner");
field.setAccessible(true); field.setAccessible(true);
assertThat((Boolean) field.get(application), equalTo(false)); assertThat((Boolean) field.get(this.application), equalTo(false));
} }
private static Matcher<? super ConfigurableEnvironment> containsPropertySource( private static Matcher<? super ConfigurableEnvironment> containsPropertySource(