Replace confusing '+' profile syntax.
Replace the confusing `spring.profiles.active` `+` syntax with a new `spring.profiles.include` property. Fixes gh-483, Fixes gh-534
This commit is contained in:
parent
7be2d97d49
commit
a79ff19b00
|
|
@ -534,10 +534,10 @@ be marked with `@Profile` to limit when it is loaded:
|
|||
}
|
||||
----
|
||||
|
||||
Spring Boot takes this a stage further, in that you can use a `spring.profiles.active`
|
||||
`Environment` property to specify which profiles are active. You can specify the property
|
||||
in any of the usual ways, for example you could include it in your
|
||||
`application.properties`:
|
||||
In the normal Spring way, you can use a `spring.profiles.active`
|
||||
`Environment` property to specify which profiles are active. You can
|
||||
specify the property in any of the usual ways, for example you could
|
||||
include it in your `application.properties`:
|
||||
|
||||
[source,properties,indent=0]
|
||||
----
|
||||
|
|
@ -556,7 +556,10 @@ active profiles in `application.properties` then *replace* them using the comman
|
|||
switch.
|
||||
|
||||
Sometimes it is useful to have profile specific properties that *add* to the active
|
||||
profiles rather than replace them. The `+` prefix can be used to add active profiles.
|
||||
profiles rather than replace them. The `spring.profiles.include` property can be used
|
||||
to unconditionally add active profiles. The `SpringApplication` entry point also has
|
||||
a Java API for setting additional profiles (i.e. on top of those activated by the
|
||||
`spring.profiles.active` property): see the `setAdditionalProfiles()` method.
|
||||
|
||||
For example, when an application with following properties is run using the switch
|
||||
`--spring.profiles.active=prod` the `proddb` and `prodmq` profiles will also be activated:
|
||||
|
|
@ -567,7 +570,7 @@ For example, when an application with following properties is run using the swit
|
|||
my.property: fromyamlfile
|
||||
---
|
||||
spring.profiles: prod
|
||||
spring.profiles.active: +proddb,+prodmq
|
||||
spring.profiles.include: proddb,prodmq
|
||||
----
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -92,6 +92,8 @@ public class ConfigFileApplicationListener implements
|
|||
|
||||
private static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
|
||||
|
||||
private static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";
|
||||
|
||||
private static final String CONFIG_NAME_PROPERTY = "spring.config.name";
|
||||
|
||||
private static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
|
||||
|
|
@ -274,9 +276,9 @@ public class ConfigFileApplicationListener implements
|
|||
|
||||
if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) {
|
||||
// Any pre-existing active profiles set via property sources (e.g. System
|
||||
// properties) take precedence over those added in config files (unless
|
||||
// latter are prefixed with "+").
|
||||
addActiveProfiles(this.environment.getProperty(ACTIVE_PROFILES_PROPERTY));
|
||||
// properties) take precedence over those added in config files.
|
||||
maybeActivateProfiles(this.environment
|
||||
.getProperty(ACTIVE_PROFILES_PROPERTY));
|
||||
}
|
||||
else {
|
||||
// Pre-existing active profiles set via Environment.setActiveProfiles()
|
||||
|
|
@ -330,36 +332,46 @@ public class ConfigFileApplicationListener implements
|
|||
PropertySource<?> propertySource = this.propertiesLoader.load(resource,
|
||||
name, profile);
|
||||
if (propertySource != null) {
|
||||
addActiveProfiles(propertySource
|
||||
maybeActivateProfiles(propertySource
|
||||
.getProperty(ACTIVE_PROFILES_PROPERTY));
|
||||
addIncludeProfiles(propertySource
|
||||
.getProperty(INCLUDE_PROFILES_PROPERTY));
|
||||
}
|
||||
return propertySource;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addActiveProfiles(Object property) {
|
||||
String profiles = (property == null ? null : property.toString());
|
||||
boolean profilesNotActivatedWhenCalled = !this.activatedProfiles;
|
||||
for (String profile : asResolvedSet(profiles, null)) {
|
||||
// A profile name prefixed with "+" is always added even if it is
|
||||
// activated in a config file (without the "+" it can be disabled
|
||||
// by an explicit Environment property set before the file was
|
||||
// processed).
|
||||
boolean addition = profile.startsWith("+");
|
||||
profile = (addition ? profile.substring(1) : profile);
|
||||
if (profilesNotActivatedWhenCalled || addition) {
|
||||
this.profiles.add(profile);
|
||||
if (!this.environment.acceptsProfiles(profile)) {
|
||||
// If it's already accepted we assume the order was set
|
||||
// intentionally
|
||||
prependProfile(this.environment, profile);
|
||||
}
|
||||
private void maybeActivateProfiles(Object value) {
|
||||
if (!this.activatedProfiles == true) {
|
||||
Set<String> profiles = getProfilesForValue(value);
|
||||
activateProfiles(profiles);
|
||||
if (profiles.size() > 0) {
|
||||
this.activatedProfiles = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addIncludeProfiles(Object value) {
|
||||
Set<String> profiles = getProfilesForValue(value);
|
||||
activateProfiles(profiles);
|
||||
}
|
||||
|
||||
private Set<String> getProfilesForValue(Object property) {
|
||||
return asResolvedSet((property == null ? null : property.toString()), null);
|
||||
}
|
||||
|
||||
private void activateProfiles(Set<String> profiles) {
|
||||
for (String profile : profiles) {
|
||||
this.profiles.add(profile);
|
||||
if (!this.environment.acceptsProfiles(profile)) {
|
||||
// If it's already accepted we assume the order was set
|
||||
// intentionally
|
||||
prependProfile(this.environment, profile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void prependProfile(ConfigurableEnvironment environment, String profile) {
|
||||
Set<String> profiles = new LinkedHashSet<String>();
|
||||
environment.getActiveProfiles(); // ensure they are initialized
|
||||
|
|
|
|||
|
|
@ -433,8 +433,8 @@ public class ConfigFileApplicationListenerTests {
|
|||
SpringApplication application = new SpringApplication(Config.class);
|
||||
application.setWebEnvironment(false);
|
||||
ConfigurableApplicationContext context = application
|
||||
.run("--spring.profiles.active=activateprofile");
|
||||
assertThat(context.getEnvironment(), acceptsProfiles("activateprofile"));
|
||||
.run("--spring.profiles.active=includeprofile");
|
||||
assertThat(context.getEnvironment(), acceptsProfiles("includeprofile"));
|
||||
assertThat(context.getEnvironment(), acceptsProfiles("specific"));
|
||||
assertThat(context.getEnvironment(), acceptsProfiles("morespecific"));
|
||||
assertThat(context.getEnvironment(), acceptsProfiles("yetmorespecific"));
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
spring.profiles.active=+specific
|
||||
|
|
@ -0,0 +1 @@
|
|||
spring.profiles.include=specific
|
||||
|
|
@ -1 +1,2 @@
|
|||
spring.profiles.active=+yetmorespecific,missing
|
||||
spring.profiles.include=yetmorespecific
|
||||
spring.profiles.active=missing
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
spring.profiles.active=+morespecific
|
||||
spring.profiles.include=morespecific
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
spring:
|
||||
profiles:
|
||||
active: +a
|
||||
include: a
|
||||
|
|
|
|||
Loading…
Reference in New Issue