Commit e10856fd authored by Chris Beams's avatar Chris Beams Committed by Dave Syer

Refactor SpringApplication Environment config hooks

Per discussion: fixes gh-467
parent d4083e46
...@@ -269,8 +269,7 @@ public class SpringApplication { ...@@ -269,8 +269,7 @@ public class SpringApplication {
try { try {
// Create and configure the environment // Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment(); ConfigurableEnvironment environment = getOrCreateEnvironment();
addPropertySources(environment, args); configureEnvironment(environment, args);
setupProfiles(environment);
for (SpringApplicationRunListener runListener : runListeners) { for (SpringApplicationRunListener runListener : runListeners) {
runListener.environmentPrepared(environment); runListener.environmentPrepared(environment);
} }
...@@ -382,11 +381,28 @@ public class SpringApplication { ...@@ -382,11 +381,28 @@ public class SpringApplication {
} }
/** /**
* Add any {@link PropertySource}s to the environment. * Template method delegating to
* @param environment the environment * {@link #configurePropertySources(ConfigurableEnvironment, String[])} and
* @param args run arguments * {@link #configureProfiles(ConfigurableEnvironment, String[])} in that order. Override
* this method for complete control over Environment customization, or one of the above
* for fine-grained control over property sources or profiles, respectively.
* @param environment this application's environment
* @param args arguments passed to the {@code run} method
* @see #configurePropertySources(ConfigurableEnvironment, String[])
* @see #configureProfiles(ConfigurableEnvironment, String[])
*/
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
configurePropertySources(environment, args);
configureProfiles(environment, args);
}
/**
* Add, remove or re-order any {@link PropertySource}s in this application's environment.
* @param environment this application's environment
* @param args arguments passed to the {@code run} method
* @see #configureEnvironment(ConfigurableEnvironment, String[])
*/ */
protected void addPropertySources(ConfigurableEnvironment environment, String[] args) { protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
MutablePropertySources sources = environment.getPropertySources(); MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties", sources.addLast(new MapPropertySource("defaultProperties",
...@@ -409,10 +425,14 @@ public class SpringApplication { ...@@ -409,10 +425,14 @@ public class SpringApplication {
} }
/** /**
* Setup any active profiles on the environment. * Configure which profiles are active (or active by default) for this application environment.
* @param environment the environment to configure * Consider overriding this method to programmatically enforce profile rules and semantics,
* such as ensuring mutual exclusivity of profiles (e.g. 'dev' OR 'prod', but never both).
* @param environment this application's environment
* @param args arguments passed to the {@code run} method
* @see #configureEnvironment(ConfigurableEnvironment, String[])
*/ */
protected void setupProfiles(ConfigurableEnvironment environment) { protected void configureProfiles(ConfigurableEnvironment environment, String[] args) {
Set<String> profiles = new LinkedHashSet<String>(); Set<String> profiles = new LinkedHashSet<String>();
environment.getActiveProfiles(); // ensure they are initialized environment.getActiveProfiles(); // ensure they are initialized
// But these ones should go first (last wins in a property key clash) // But these ones should go first (last wins in a property key clash)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment