Commit fce1c3ff authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '1.5.x'

parents 1f0672f6 7d247a71
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -63,8 +63,21 @@ public class PropertySourcesPropertyValues implements PropertyValues {
* @param propertySources a PropertySources instance
*/
public PropertySourcesPropertyValues(PropertySources propertySources) {
this(propertySources, true);
}
/**
* Create a new PropertyValues from the given PropertySources that will optionally
* resolve placeholders.
* @param propertySources a PropertySources instance
* @param resolvePlaceholders {@code true} if placeholders should be resolved,
* otherwise {@code false}
* @since 1.5.2
*/
public PropertySourcesPropertyValues(PropertySources propertySources,
boolean resolvePlaceholders) {
this(propertySources, (Collection<String>) null, PropertyNamePatternsMatcher.ALL,
true);
resolvePlaceholders);
}
/**
......
......@@ -481,10 +481,20 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
SpringProfiles springProfiles = new SpringProfiles();
RelaxedDataBinder dataBinder = new RelaxedDataBinder(springProfiles,
"spring.profiles");
dataBinder.bind(new PropertySourcesPropertyValues(propertySources));
dataBinder.bind(new PropertySourcesPropertyValues(propertySources, false));
springProfiles.setActive(resolvePlaceholders(springProfiles.getActive()));
springProfiles.setInclude(resolvePlaceholders(springProfiles.getInclude()));
return springProfiles;
}
private List<String> resolvePlaceholders(List<String> values) {
List<String> resolved = new ArrayList<String>();
for (String value : values) {
resolved.add(this.environment.resolvePlaceholders(value));
}
return resolved;
}
private void maybeActivateProfiles(Set<Profile> profiles) {
if (this.activatedProfiles) {
if (!profiles.isEmpty()) {
......
......@@ -23,7 +23,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import ch.qos.logback.classic.BasicConfigurator;
......@@ -804,6 +806,20 @@ public class ConfigFileApplicationListenerTests {
.isFalse();
}
@Test
public void activeProfilesCanBeConfiguredUsingPlaceholdersResolvedAgainstTheEnvironment()
throws Exception {
Map<String, Object> source = new HashMap<String, Object>();
source.put("activeProfile", "testPropertySource");
org.springframework.core.env.PropertySource<?> propertySource = new MapPropertySource(
"test", source);
this.environment.getPropertySources().addLast(propertySource);
this.initializer.setSearchNames("testactiveprofiles");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles())
.containsExactly("testPropertySource");
}
private Condition<ConfigurableEnvironment> matchingPropertySource(
final String sourceName) {
return new Condition<ConfigurableEnvironment>(
......
spring.profiles.active=${activeProfile:propertiesfile}
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