From 8922a6be4aa17495bc3303bc8898b797d2fc573f Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 6 Nov 2013 14:16:31 +0000 Subject: [PATCH] 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 --- .../boot/bind/PropertySourcesPropertyValues.java | 8 +++++++- .../boot/bind/PropertySourcesPropertyValuesTests.java | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java index b8eed8b8291..3e92db48088 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java @@ -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)); } diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java index 2a183c70082..c8bb7153f59 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesPropertyValuesTests.java @@ -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 + . singletonMap("something", "${nonexistent}"))); + binder.bind(new PropertySourcesPropertyValues(this.propertySources)); + assertEquals("bar", target.getName()); + } + public static class TestBean { private String name;