diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index eb6d978221b..d3ca0b166ce 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -38,6 +38,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.context.properties.source.ConfigurationPropertyState; import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.env.Environment; import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.util.Assert; @@ -244,7 +245,18 @@ public class Binder { return bindAggregate(name, target, handler, context, aggregateBinder); } if (property != null) { - return bindProperty(name, target, handler, context, property); + try { + return bindProperty(name, target, handler, context, property); + } + catch (ConverterNotFoundException ex) { + // We might still be able to bind it as a bean + Object bean = bindBean(name, target, handler, context, + allowRecursiveBinding); + if (bean != null) { + return bean; + } + throw ex; + } } return bindBean(name, target, handler, context, allowRecursiveBinding); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index fb21fc4e1fb..bb802e5c365 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -190,6 +190,18 @@ public class BinderTests { assertThat(result.getValue()).isEqualTo("bar"); } + @Test + public void bindToJavaBeanWhenHasPropertyWithSameNameShouldStillBind() + throws Exception { + // gh-10945 + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo", "boom"); + source.put("foo.value", "bar"); + this.sources.add(source); + JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.class)).get(); + assertThat(result.getValue()).isEqualTo("bar"); + } + @Test public void bindToJavaBeanShouldTriggerOnSuccess() throws Exception { this.sources