Merge branch '1.5.x'
This commit is contained in:
commit
4a10cfe112
|
@ -127,8 +127,8 @@ content into your application; rather pick only the properties that you need.
|
||||||
spring.pid.file= # Location of the PID file to write (if ApplicationPidFileWriter is used).
|
spring.pid.file= # Location of the PID file to write (if ApplicationPidFileWriter is used).
|
||||||
|
|
||||||
# PROFILES
|
# PROFILES
|
||||||
spring.profiles.active= # Comma-separated list of <<howto-set-active-spring-profiles,active profiles>>.
|
spring.profiles.active= # Comma-separated list (or list if using YAML) of <<howto-set-active-spring-profiles,active profiles>>.
|
||||||
spring.profiles.include= # Unconditionally activate the specified comma separated profiles.
|
spring.profiles.include= # Unconditionally activate the specified comma separated profiles (or list of profiles if using YAML).
|
||||||
|
|
||||||
# SENDGRID ({sc-spring-boot-autoconfigure}/sendgrid/SendGridAutoConfiguration.{sc-ext}[SendGridAutoConfiguration])
|
# SENDGRID ({sc-spring-boot-autoconfigure}/sendgrid/SendGridAutoConfiguration.{sc-ext}[SendGridAutoConfiguration])
|
||||||
spring.sendgrid.api-key= # SendGrid api key (alternative to username/password)
|
spring.sendgrid.api-key= # SendGrid api key (alternative to username/password)
|
||||||
|
|
|
@ -1173,7 +1173,9 @@ For example, when an application with following properties is run using the swit
|
||||||
my.property: fromyamlfile
|
my.property: fromyamlfile
|
||||||
---
|
---
|
||||||
spring.profiles: prod
|
spring.profiles: prod
|
||||||
spring.profiles.include: proddb,prodmq
|
spring.profiles.include:
|
||||||
|
- proddb
|
||||||
|
- prodmq
|
||||||
----
|
----
|
||||||
|
|
||||||
NOTE: Remember that the `spring.profiles` property can be defined in a YAML document
|
NOTE: Remember that the `spring.profiles` property can be defined in a YAML document
|
||||||
|
|
|
@ -36,6 +36,8 @@ 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.bind.PropertySourcesPropertyValues;
|
||||||
|
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||||
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.context.event.ApplicationPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationPreparedEvent;
|
||||||
|
@ -55,6 +57,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.EnumerablePropertySource;
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
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.env.PropertySources;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
@ -390,8 +393,8 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
}
|
}
|
||||||
// Any pre-existing active profiles set via property sources (e.g. System
|
// Any pre-existing active profiles set via property sources (e.g. System
|
||||||
// properties) take precedence over those added in config files.
|
// properties) take precedence over those added in config files.
|
||||||
Set<Profile> activeProfiles = getProfilesForValue(
|
Set<Profile> activeProfiles = bindSpringProfiles(
|
||||||
this.environment.getProperty(ACTIVE_PROFILES_PROPERTY));
|
this.environment.getPropertySources()).getActiveProfiles();
|
||||||
maybeActivateProfiles(activeProfiles);
|
maybeActivateProfiles(activeProfiles);
|
||||||
return activeProfiles;
|
return activeProfiles;
|
||||||
}
|
}
|
||||||
|
@ -506,12 +509,23 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleProfileProperties(PropertySource<?> propertySource) {
|
private void handleProfileProperties(PropertySource<?> propertySource) {
|
||||||
Set<Profile> activeProfiles = getProfilesForValue(
|
SpringProfiles springProfiles = bindSpringProfiles(propertySource);
|
||||||
propertySource.getProperty(ACTIVE_PROFILES_PROPERTY));
|
maybeActivateProfiles(springProfiles.getActiveProfiles());
|
||||||
maybeActivateProfiles(activeProfiles);
|
addProfiles(springProfiles.getIncludeProfiles());
|
||||||
Set<Profile> includeProfiles = getProfilesForValue(
|
}
|
||||||
propertySource.getProperty(INCLUDE_PROFILES_PROPERTY));
|
|
||||||
addProfiles(includeProfiles);
|
private SpringProfiles bindSpringProfiles(PropertySource<?> propertySource) {
|
||||||
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
|
propertySources.addFirst(propertySource);
|
||||||
|
return bindSpringProfiles(propertySources);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SpringProfiles bindSpringProfiles(PropertySources propertySources) {
|
||||||
|
SpringProfiles springProfiles = new SpringProfiles();
|
||||||
|
RelaxedDataBinder dataBinder = new RelaxedDataBinder(springProfiles,
|
||||||
|
"spring.profiles");
|
||||||
|
dataBinder.bind(new PropertySourcesPropertyValues(propertySources));
|
||||||
|
return springProfiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeActivateProfiles(Set<Profile> profiles) {
|
private void maybeActivateProfiles(Set<Profile> profiles) {
|
||||||
|
@ -540,16 +554,6 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<Profile> getProfilesForValue(Object property) {
|
|
||||||
String value = (property == null ? null : property.toString());
|
|
||||||
Set<String> profileNames = asResolvedSet(value, null);
|
|
||||||
Set<Profile> profiles = new LinkedHashSet<Profile>();
|
|
||||||
for (String profileName : profileNames) {
|
|
||||||
profiles.add(new Profile(profileName));
|
|
||||||
}
|
|
||||||
return profiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addProfiles(Set<Profile> profiles) {
|
private void addProfiles(Set<Profile> profiles) {
|
||||||
for (Profile profile : profiles) {
|
for (Profile profile : profiles) {
|
||||||
this.profiles.add(profile);
|
this.profiles.add(profile);
|
||||||
|
@ -750,4 +754,48 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holder for {@code spring.profiles} properties.
|
||||||
|
*/
|
||||||
|
static final class SpringProfiles {
|
||||||
|
|
||||||
|
private List<String> active = new ArrayList<String>();
|
||||||
|
|
||||||
|
private List<String> include = new ArrayList<String>();
|
||||||
|
|
||||||
|
public List<String> getActive() {
|
||||||
|
return this.active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(List<String> active) {
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getInclude() {
|
||||||
|
return this.include;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInclude(List<String> include) {
|
||||||
|
this.include = include;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Profile> getActiveProfiles() {
|
||||||
|
return asProfileSet(this.active);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Profile> getIncludeProfiles() {
|
||||||
|
return asProfileSet(this.include);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Profile> asProfileSet(List<String> profileNames) {
|
||||||
|
List<Profile> profiles = new ArrayList<Profile>();
|
||||||
|
for (String profileName : profileNames) {
|
||||||
|
profiles.add(new Profile(profileName));
|
||||||
|
}
|
||||||
|
Collections.reverse(profiles);
|
||||||
|
return new LinkedHashSet<Profile>(profiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -536,6 +536,14 @@ public class ConfigFileApplicationListenerTests {
|
||||||
"healthcheck");
|
"healthcheck");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void yamlSetsMultiProfilesWhenListProvided() throws Exception {
|
||||||
|
this.initializer.setSearchNames("testsetmultiprofileslist");
|
||||||
|
this.initializer.postProcessEnvironment(this.environment, this.application);
|
||||||
|
assertThat(this.environment.getActiveProfiles()).containsExactly("dev",
|
||||||
|
"healthcheck");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void yamlSetsMultiProfilesWithWhitespace() throws Exception {
|
public void yamlSetsMultiProfilesWithWhitespace() throws Exception {
|
||||||
this.initializer.setSearchNames("testsetmultiprofileswhitespace");
|
this.initializer.setSearchNames("testsetmultiprofileswhitespace");
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
profiles:
|
||||||
|
active:
|
||||||
|
- dev
|
||||||
|
- healthcheck
|
Loading…
Reference in New Issue