diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index d4d61f5c8aa..1cdceca65df 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -189,6 +189,7 @@ public class Binder { protected final T bind(ConfigurationPropertyName name, Bindable target, BindHandler handler, Context context) { + context.clearConfigurationProperty(); try { if (!handler.onStart(name, target, context)) { return null; @@ -433,6 +434,10 @@ public class Binder { this.configurationProperty = configurationProperty; } + void clearConfigurationProperty() { + this.configurationProperty = null; + } + @Override public PlaceholdersResolver getPlaceholdersResolver() { return Binder.this.placeholdersResolver; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index 48d135e83f5..71b6ebe9edf 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.assertj.core.matcher.AssertionMatcher; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -214,10 +215,31 @@ public class BinderTests { assertThat(result.toString()).isEqualTo("2014-04-01"); } + @Test + public void bindExceptionWhenBeanBindingFailsShouldHaveNullConfigurationProperty() throws Exception { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo.value", "hello"); + source.put("foo.items", "bar,baz"); + this.sources + .add(source); + Bindable target = Bindable.of(JavaBean.class); + this.thrown.expect(BindException.class); + this.thrown.expect(new AssertionMatcher() { + @Override + public void assertion(BindException ex) throws AssertionError { + assertThat(ex.getCause().getMessage()).isEqualTo("No setter found for property: items"); + assertThat(ex.getProperty()).isNull(); + } + }); + this.binder.bind("foo", target); + } + public static class JavaBean { private String value; + private List items = Collections.emptyList(); + public String getValue() { return this.value; } @@ -226,6 +248,9 @@ public class BinderTests { this.value = value; } + public List getItems() { + return this.items; + } } public enum ExampleEnum {