Allow use of underscores in @ConfigurationProperties prefix

Currently the PropertiesConfigurationFilter filters them out.
Thus when deploying to IAAS these environment variables are ignored.

Fixes gh-154
pull/165/head
Glenn Renfro 11 years ago committed by Dave Syer
parent 3a37ddb237
commit 6cf3b91b2e

@ -258,6 +258,7 @@ public class PropertiesConfigurationFactory<T> implements FactoryBean<T>,
names.add(name);
names.add(name + ".*");
names.add(name + "_*");
names.add("*_"+name);
}
}
}

@ -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");
@ -130,6 +138,16 @@ public class PropertiesConfigurationFactoryTests {
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;
}

@ -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);
@ -287,6 +298,10 @@ public class EnableConfigurationPropertiesTests {
protected static class StrictTestConfiguration {
}
@Configuration
@EnableConfigurationProperties(EmbeddedTestProperties.class)
protected static class EmbeddedTestConfiguration {
}
@Configuration
@EnableConfigurationProperties(IgnoreNestedTestProperties.class)
protected static class IgnoreNestedTestConfiguration {
@ -410,6 +425,10 @@ public class EnableConfigurationPropertiesTests {
@ConfigurationProperties(ignoreUnknownFields = false)
protected static class StrictTestProperties extends TestProperties {
}
@ConfigurationProperties(name = "spring.foo")
protected static class EmbeddedTestProperties extends TestProperties {
}
@ConfigurationProperties(ignoreUnknownFields = false, ignoreNestedProperties = true)

@ -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;
}
}

Loading…
Cancel
Save