Applying RelaxedNames before filtering PropertySources

... to allow for OS_VAR to be bound to a @ConfigurationPropertes("os")
class with field "var".

Fixes gh-387, Fixes gh-391
pull/382/merge
joschs 11 years ago committed by Dave Syer
parent 1f69ef69cb
commit b7802f98bb

@ -253,17 +253,16 @@ public class PropertiesConfigurationFactory<T> implements FactoryBean<T>,
if (this.target != null) { if (this.target != null) {
PropertyDescriptor[] descriptors = BeanUtils PropertyDescriptor[] descriptors = BeanUtils
.getPropertyDescriptors(this.target.getClass()); .getPropertyDescriptors(this.target.getClass());
String[] prefixes = this.targetName != null ? new String[] { String prefix = this.targetName != null ? this.targetName + "." : "";
this.targetName + ".", this.targetName + "_" } : new String[] { "" };
String[] suffixes = new String[] { ".*", "_*" }; String[] suffixes = new String[] { ".*", "_*" };
for (PropertyDescriptor descriptor : descriptors) { for (PropertyDescriptor descriptor : descriptors) {
String name = descriptor.getName(); String name = descriptor.getName();
if (!name.equals("class")) { if (!name.equals("class")) {
for (String prefix : prefixes) { for(String relaxedName : new RelaxedNames(prefix + name)) {
names.add(prefix + name); names.add(relaxedName);
patterns.add(prefix + name); patterns.add(relaxedName);
for (String suffix : suffixes) { for (String suffix : suffixes) {
patterns.add(prefix + name + suffix); patterns.add(relaxedName + suffix);
} }
} }
} }

@ -101,6 +101,16 @@ public class EnableConfigurationPropertiesTests {
assertEquals("bar", this.context.getBean(NestedProperties.class).nested.name); assertEquals("bar", this.context.getBean(NestedProperties.class).nested.name);
} }
@Test
public void testNestedOsEnvironmentVariableWithUnderscore() {
EnvironmentTestUtils.addEnvironment(this.context, "NAME:foo", "NESTED_NAME:bar");
this.context.register(NestedConfiguration.class);
this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(NestedProperties.class).length);
assertEquals("foo", this.context.getBean(NestedProperties.class).name);
assertEquals("bar", this.context.getBean(NestedProperties.class).nested.name);
}
@Test @Test
public void testStrictPropertiesBinding() { public void testStrictPropertiesBinding() {
removeSystemProperties(); removeSystemProperties();
@ -122,6 +132,16 @@ public class EnableConfigurationPropertiesTests {
assertEquals("foo", this.context.getBean(TestProperties.class).name); assertEquals("foo", this.context.getBean(TestProperties.class).name);
} }
@Test
public void testOsEnvironmentVariableEmbeddedBinding() {
EnvironmentTestUtils.addEnvironment(this.context, "SPRING_FOO_NAME:foo");
this.context.register(EmbeddedTestConfiguration.class);
this.context.refresh();
assertEquals(1,
this.context.getBeanNamesForType(EmbeddedTestProperties.class).length);
assertEquals("foo", this.context.getBean(TestProperties.class).name);
}
@Test @Test
public void testIgnoreNestedPropertiesBinding() { public void testIgnoreNestedPropertiesBinding() {
removeSystemProperties(); removeSystemProperties();

Loading…
Cancel
Save