diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java index ac1e88a10f..6551b9bdb8 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java @@ -258,6 +258,7 @@ public class PropertiesConfigurationFactory implements FactoryBean, names.add(name); names.add(name + ".*"); names.add(name + "_*"); + names.add("*_"+name); } } } diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java index eb571506d1..6c7cdeaf04 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java @@ -68,6 +68,14 @@ public class PropertiesConfigurationFactoryTests { assertEquals("blah", foo.name); } + + @Test + public void testUnderscore() throws Exception { + Foo foo = createFoo("spring_foo_baz: blah\nname: blah"); + assertEquals("blah", foo.spring_foo_baz); + assertEquals("blah", foo.name); + } + @Test public void testUnknownPropertyOkByDefault() throws Exception { Foo foo = createFoo("hi: hello\nname: foo\nbar: blah"); @@ -129,6 +137,16 @@ public class PropertiesConfigurationFactoryTests { private String name; private String bar; + + private String spring_foo_baz; + + public String getSpringFooBaz() { + return spring_foo_baz; + } + + public void setSpringFooBaz(String spring_foo_baz) { + this.spring_foo_baz = spring_foo_baz; + } public String getName() { return this.name; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java index 2075324dc2..a236ac1ac1 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java @@ -109,6 +109,17 @@ public class EnableConfigurationPropertiesTests { assertEquals("foo", this.context.getBean(TestProperties.class).name); } + @Test + public void testPropertiesEmbeddedBinding() { + this.context.register(EmbeddedTestConfiguration.class); + TestUtils.addEnviroment(this.context, "spring_foo_name:foo"); + this.context.refresh(); + assertEquals(1, + this.context.getBeanNamesForType(EmbeddedTestProperties.class).length); + assertEquals("foo", this.context.getBean(TestProperties.class).name); + } + + @Test public void testIgnoreNestedPropertiesBinding() { this.context.register(IgnoreNestedTestConfiguration.class); @@ -286,7 +297,11 @@ public class EnableConfigurationPropertiesTests { @EnableConfigurationProperties(StrictTestProperties.class) protected static class StrictTestConfiguration { } - + + @Configuration + @EnableConfigurationProperties(EmbeddedTestProperties.class) + protected static class EmbeddedTestConfiguration { + } @Configuration @EnableConfigurationProperties(IgnoreNestedTestProperties.class) protected static class IgnoreNestedTestConfiguration { @@ -411,7 +426,11 @@ public class EnableConfigurationPropertiesTests { protected static class StrictTestProperties extends TestProperties { } + @ConfigurationProperties(name = "spring.foo") + protected static class EmbeddedTestProperties extends TestProperties { + } + @ConfigurationProperties(ignoreUnknownFields = false, ignoreNestedProperties = true) protected static class IgnoreNestedTestProperties extends TestProperties { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/test/EnableConfigurationPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/context/test/EnableConfigurationPropertiesTests.java index bd32f783af..fe0d455bcb 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/test/EnableConfigurationPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/test/EnableConfigurationPropertiesTests.java @@ -48,8 +48,9 @@ public class EnableConfigurationPropertiesTests { .getEnvironment() .getPropertySources() .addFirst( - new PropertiesPropertySource("props", - getProperties("external.name=foo\nanother.name=bar"))); + new PropertiesPropertySource( + "props", + getProperties("external.name=foo\nanother.name=bar\nspring_test_external_val=baz"))); } @After @@ -59,6 +60,13 @@ public class EnableConfigurationPropertiesTests { } } + @Test + public void testUnderscoresInPrefix() throws Exception { + this.context.register(SystemExampleConfig.class); + this.context.refresh(); + assertEquals("baz", this.context.getBean(SystemEnvVar.class).getVal()); + } + @Test public void testSimpleAutoConfig() throws Exception { this.context.register(ExampleConfig.class); @@ -105,6 +113,11 @@ public class EnableConfigurationPropertiesTests { public static class FurtherExampleConfig { } + @EnableConfigurationProperties({ SystemEnvVar.class }) + @Configuration + public static class SystemExampleConfig { + } + @ConfigurationProperties(name = "external") public static class External { @@ -132,4 +145,18 @@ public class EnableConfigurationPropertiesTests { this.name = name; } } + + @ConfigurationProperties(name = "spring_test_external") + public static class SystemEnvVar { + public String getVal() { + return this.val; + } + + public void setVal(String val) { + this.val = val; + } + + private String val; + + } }