diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 74903458bd9..c54de9dbc1f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -269,8 +269,7 @@ public class SpringApplication { try { // Create and configure the environment ConfigurableEnvironment environment = getOrCreateEnvironment(); - addPropertySources(environment, args); - setupProfiles(environment); + configureEnvironment(environment, args); for (SpringApplicationRunListener runListener : runListeners) { runListener.environmentPrepared(environment); } @@ -382,11 +381,28 @@ public class SpringApplication { } /** - * Add any {@link PropertySource}s to the environment. - * @param environment the environment - * @param args run arguments + * Template method delegating to + * {@link #configurePropertySources(ConfigurableEnvironment, String[])} and + * {@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 addPropertySources(ConfigurableEnvironment environment, String[] args) { + 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 configurePropertySources(ConfigurableEnvironment environment, String[] args) { MutablePropertySources sources = environment.getPropertySources(); if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) { sources.addLast(new MapPropertySource("defaultProperties", @@ -409,10 +425,14 @@ public class SpringApplication { } /** - * Setup any active profiles on the environment. - * @param environment the environment to configure + * Configure which profiles are active (or active by default) for this application environment. + * 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 profiles = new LinkedHashSet(); environment.getActiveProfiles(); // ensure they are initialized // But these ones should go first (last wins in a property key clash)