From b7802f98bb83b1de201be8a9e5affdb0f0651ec1 Mon Sep 17 00:00:00 2001 From: joschs Date: Sat, 22 Feb 2014 15:58:40 +0100 Subject: [PATCH] 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 --- .../bind/PropertiesConfigurationFactory.java | 11 +++++----- .../EnableConfigurationPropertiesTests.java | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) 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 487c3bd338..69bd62b833 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 @@ -253,17 +253,16 @@ public class PropertiesConfigurationFactory implements FactoryBean, if (this.target != null) { PropertyDescriptor[] descriptors = BeanUtils .getPropertyDescriptors(this.target.getClass()); - String[] prefixes = this.targetName != null ? new String[] { - this.targetName + ".", this.targetName + "_" } : new String[] { "" }; + String prefix = this.targetName != null ? this.targetName + "." : ""; String[] suffixes = new String[] { ".*", "_*" }; for (PropertyDescriptor descriptor : descriptors) { String name = descriptor.getName(); if (!name.equals("class")) { - for (String prefix : prefixes) { - names.add(prefix + name); - patterns.add(prefix + name); + for(String relaxedName : new RelaxedNames(prefix + name)) { + names.add(relaxedName); + patterns.add(relaxedName); for (String suffix : suffixes) { - patterns.add(prefix + name + suffix); + patterns.add(relaxedName + suffix); } } } 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 fcac2d5644..f4ea7db23d 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 @@ -101,6 +101,16 @@ public class EnableConfigurationPropertiesTests { 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 public void testStrictPropertiesBinding() { removeSystemProperties(); @@ -122,6 +132,16 @@ public class EnableConfigurationPropertiesTests { 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 public void testIgnoreNestedPropertiesBinding() { removeSystemProperties();