From 1be040170c46423dd35f46ac8b5e765fbc77780a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 7 Oct 2013 09:10:02 -0400 Subject: [PATCH] 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. --- .../builder/SpringApplicationBuilder.java | 5 ++-- ...nfigFileApplicationContextInitializer.java | 25 ++++++++++++++----- .../SpringApplicationBuilderTests.java | 2 +- .../test/resources/enableprofile.properties | 1 + 4 files changed, 23 insertions(+), 10 deletions(-) rename spring-boot/src/test/java/org/springframework/boot/{ => builder}/SpringApplicationBuilderTests.java (99%) diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index a010cc64d6..8b96d5c0b3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -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 diff --git a/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java index 5afeb8aad0..e5a759a756 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java @@ -126,17 +126,30 @@ public class ConfigFileApplicationContextInitializer implements List candidates = getCandidateLocations(); Collections.reverse(candidates); + List> sources = new ArrayList>(); // 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 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 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, diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java similarity index 99% rename from spring-boot/src/test/java/org/springframework/boot/SpringApplicationBuilderTests.java rename to spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index 37287eb140..d7a4cbe1c4 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -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; diff --git a/spring-boot/src/test/resources/enableprofile.properties b/spring-boot/src/test/resources/enableprofile.properties index ae6059d91f..01c6230de7 100644 --- a/spring-boot/src/test/resources/enableprofile.properties +++ b/spring-boot/src/test/resources/enableprofile.properties @@ -1 +1,2 @@ spring.profiles.active=myprofile +my.property=frompropertiesfile