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
This commit is contained in:
Dave Syer
2013-11-06 14:16:31 +00:00
parent 7cf98d15f2
commit 8922a6be4a
2 changed files with 17 additions and 1 deletions

View File

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

View File

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