diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java index 0bbbfcc8e3..5bb7cd1bb9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java @@ -88,8 +88,10 @@ public class PropertySourcesPropertyValues implements PropertyValues { catch (RuntimeException ex) { // Probably could not resolve placeholders, ignore it here } - this.propertyValues.put(propertyName, new PropertyValue( - propertyName, value)); + if (!this.propertyValues.containsKey(propertyName)) { + this.propertyValues.put(propertyName, new PropertyValue( + propertyName, value)); + } } } } @@ -99,13 +101,13 @@ public class PropertySourcesPropertyValues implements PropertyValues { for (String propertyName : exacts) { Object value; value = source.getProperty(propertyName); - if (value != null) { + if (value != null && !this.propertyValues.containsKey(propertyName)) { this.propertyValues.put(propertyName, new PropertyValue( propertyName, value)); continue; } value = source.getProperty(propertyName.toUpperCase()); - if (value != null) { + if (value != null && !this.propertyValues.containsKey(propertyName)) { this.propertyValues.put(propertyName, new PropertyValue( propertyName, value)); continue; diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java new file mode 100644 index 0000000000..500d189af7 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java @@ -0,0 +1,72 @@ +package org.springframework.boot.bind; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.bind.PropertySourcesBindingTests.TestConfig; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = TestConfig.class) +@IntegrationTest +public class PropertySourcesBindingTests { + + @Value("${foo:}") + private String foo; + + @Autowired + private Wrapper properties; + + @Test + public void overridingOfPropertiesWorksAsExpected() { + assertThat(this.foo, is(this.properties.getFoo())); + } + + @Import({ SomeConfig.class }) + @PropertySources({ @PropertySource("classpath:/override.properties"), + @PropertySource("classpath:/some.properties") }) + @Configuration + @EnableConfigurationProperties(Wrapper.class) + public static class TestConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + } + + @Configuration + @PropertySources({ @PropertySource("classpath:/override.properties"), + @PropertySource("classpath:/some.properties") }) + public static class SomeConfig { + } + + @ConfigurationProperties + public static class Wrapper { + private String foo; + + public String getFoo() { + return this.foo; + } + + public void setFoo(String foo) { + this.foo = foo; + } + } + +} \ No newline at end of file diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/SimplerPropertySourcesBindingTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/SimplerPropertySourcesBindingTests.java new file mode 100644 index 0000000000..a31f684707 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/bind/SimplerPropertySourcesBindingTests.java @@ -0,0 +1,64 @@ +package org.springframework.boot.bind; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.bind.SimplerPropertySourcesBindingTests.TestConfig; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = TestConfig.class) +@IntegrationTest +public class SimplerPropertySourcesBindingTests { + + @Value("${foo:}") + private String foo; + + @Autowired + private Wrapper properties; + + @Test + public void overridingOfPropertiesWorksAsExpected() { + assertThat(this.foo, is(this.properties.getFoo())); + } + + @PropertySources({ @PropertySource("classpath:/override.properties"), + @PropertySource("classpath:/some.properties") }) + @Configuration + @EnableConfigurationProperties(Wrapper.class) + public static class TestConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + } + + @ConfigurationProperties + public static class Wrapper { + private String foo; + + public String getFoo() { + return this.foo; + } + + public void setFoo(String foo) { + this.foo = foo; + } + } + +} \ No newline at end of file diff --git a/spring-boot/src/test/resources/override.properties b/spring-boot/src/test/resources/override.properties new file mode 100644 index 0000000000..74d0a43fcc --- /dev/null +++ b/spring-boot/src/test/resources/override.properties @@ -0,0 +1 @@ +foo=bar diff --git a/spring-boot/src/test/resources/some.properties b/spring-boot/src/test/resources/some.properties new file mode 100644 index 0000000000..376216c57e --- /dev/null +++ b/spring-boot/src/test/resources/some.properties @@ -0,0 +1 @@ +foo=spam