Refactor SpringApplication Environment config hooks
Per discussion: fixes gh-467
This commit is contained in:
parent
d4083e46fe
commit
e10856fd9b
|
|
@ -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<String> profiles = new LinkedHashSet<String>();
|
||||
environment.getActiveProfiles(); // ensure they are initialized
|
||||
// But these ones should go first (last wins in a property key clash)
|
||||
|
|
|
|||
Loading…
Reference in New Issue