Skip to content

Commit 8922a6b

Browse files
author
Dave Syer
committed
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
1 parent 7cf98d1 commit 8922a6b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ public PropertySourcesPropertyValues(PropertySources propertySources) {
5555
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
5656
if (enumerable.getPropertyNames().length > 0) {
5757
for (String propertyName : enumerable.getPropertyNames()) {
58-
Object value = resolver.getProperty(propertyName);
58+
Object value = source.getProperty(propertyName);
59+
try {
60+
value = resolver.getProperty(propertyName);
61+
}
62+
catch (RuntimeException e) {
63+
// Probably could not resolve placeholders, ignore it here
64+
}
5965
this.propertyValues.put(propertyName, new PropertyValue(
6066
propertyName, value));
6167
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public void testPlaceholdersBinding() {
9090
assertEquals("bar", target.getName());
9191
}
9292

93+
@Test
94+
public void testPlaceholdersBindingWithError() {
95+
TestBean target = new TestBean();
96+
DataBinder binder = new DataBinder(target);
97+
this.propertySources.addFirst(new MapPropertySource("another", Collections
98+
.<String, Object> singletonMap("something", "${nonexistent}")));
99+
binder.bind(new PropertySourcesPropertyValues(this.propertySources));
100+
assertEquals("bar", target.getName());
101+
}
102+
93103
public static class TestBean {
94104
private String name;
95105

0 commit comments

Comments
 (0)