Browse Source

Ignore exceptions while resolving placeholders in PropertySources

When a PropertySourcesPropertyValues is used to bind Environment
values to a bean (or the SpringApplication) it tries to resolve
placeholders eagerly in the Environment. Any that fail might not
actually be a problem for users (until validation is done it's
impossible to tell even whether that value was needed for the
ongoing binding or not).

Fixed by ignoring exceptions in the PropertySourcesPropertyValues
constructor.

Fixes gh-108
pull/118/merge
Dave Syer 13 years ago
parent
commit
8922a6be4a
  1. 8
      spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java
  2. 10
      spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java

8
spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java

@ -55,7 +55,13 @@ public class PropertySourcesPropertyValues implements PropertyValues { @@ -55,7 +55,13 @@ public class PropertySourcesPropertyValues implements PropertyValues {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
if (enumerable.getPropertyNames().length > 0) {
for (String propertyName : enumerable.getPropertyNames()) {
Object value = resolver.getProperty(propertyName);
Object value = source.getProperty(propertyName);
try {
value = resolver.getProperty(propertyName);
}
catch (RuntimeException e) {
// Probably could not resolve placeholders, ignore it here
}
this.propertyValues.put(propertyName, new PropertyValue(
propertyName, value));
}

10
spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java

@ -90,6 +90,16 @@ public class PropertySourcesPropertyValuesTests { @@ -90,6 +90,16 @@ public class PropertySourcesPropertyValuesTests {
assertEquals("bar", target.getName());
}
@Test
public void testPlaceholdersBindingWithError() {
TestBean target = new TestBean();
DataBinder binder = new DataBinder(target);
this.propertySources.addFirst(new MapPropertySource("another", Collections
.<String, Object> singletonMap("something", "${nonexistent}")));
binder.bind(new PropertySourcesPropertyValues(this.propertySources));
assertEquals("bar", target.getName());
}
public static class TestBean {
private String name;

Loading…
Cancel
Save