Fix bug with ordering of property sources

Profile-specific property sources were being added last,
*after* the global values, which means they did not
override in the way that users would expect.

Fixed by storing the global property sources in an
intermediate list and applying them after the profile
specific ones.
pull/70/merge
Dave Syer 11 years ago
parent a082f2bed5
commit 1be040170c

@ -111,8 +111,6 @@ public class SpringApplicationBuilder {
if (this.running.compareAndSet(false, true)) {
synchronized (this.running) {
// If not already running copy the sources over and then run.
// this.application.setDefaultArgs(this.defaultArgs
// .toArray(new String[this.defaultArgs.size()]));
this.application.setSources(this.sources);
this.context = this.application.run(args);
}
@ -135,7 +133,8 @@ public class SpringApplicationBuilder {
child.sources(sources);
// Copy environment stuff from parent to child
child.defaultArgs(this.defaultArgs).environment(this.environment);
child.defaultArgs(this.defaultArgs.toArray(new String[this.defaultArgs.size()]))
.environment(this.environment);
child.parent = this;
// It's not possible if embedded containers are enabled to support web contexts as

@ -126,17 +126,30 @@ public class ConfigFileApplicationContextInitializer implements
List<String> candidates = getCandidateLocations();
Collections.reverse(candidates);
List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>();
// Initial load allows profiles to be activated
for (String candidate : candidates) {
load(environment, resourceLoader, candidate, null);
PropertySource<?> source = load(environment, resourceLoader, candidate, null);
if (source != null) {
sources.add(source);
}
}
// Second load for specific profiles
for (String profile : environment.getActiveProfiles()) {
for (String candidate : candidates) {
load(environment, resourceLoader, candidate, profile);
PropertySource<?> source = load(environment, resourceLoader, candidate,
profile);
if (source != null) {
environment.getPropertySources().addLast(source);
}
}
}
// Originals go at the end so they don't override the specific profiles
for (PropertySource<?> source : sources) {
environment.getPropertySources().addLast(source);
}
}
private List<String> getCandidateLocations() {
@ -154,8 +167,8 @@ public class ConfigFileApplicationContextInitializer implements
return candidates;
}
private void load(ConfigurableEnvironment environment, ResourceLoader resourceLoader,
String location, String profile) {
private PropertySource<?> load(ConfigurableEnvironment environment,
ResourceLoader resourceLoader, String location, String profile) {
location = environment.resolvePlaceholders(location);
String suffix = "." + StringUtils.getFilenameExtension(location);
@ -173,7 +186,7 @@ public class ConfigFileApplicationContextInitializer implements
Resource resource = resourceLoader.getResource(location);
PropertySource<?> propertySource = getPropertySource(resource, profile, loaders);
if (propertySource == null) {
return;
return null;
}
if (propertySource.containsProperty("spring.profiles.active")) {
Set<String> profiles = StringUtils.commaDelimitedListToSet(propertySource
@ -184,7 +197,7 @@ public class ConfigFileApplicationContextInitializer implements
}
}
environment.getPropertySources().addLast(propertySource);
return propertySource;
}
private PropertySource<?> getPropertySource(Resource resource, String profile,

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot;
package org.springframework.boot.builder;
import org.junit.After;
import org.junit.Test;

@ -1 +1,2 @@
spring.profiles.active=myprofile
my.property=frompropertiesfile

Loading…
Cancel
Save