diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java index f8071d0333..07d14a253a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/JavaBeanBinder.java @@ -126,17 +126,17 @@ class JavaBeanBinder implements BeanBinder { private void addMethod(Method method) { String name = method.getName(); int parameterCount = method.getParameterCount(); - if (name.startsWith("get") && parameterCount == 0) { + if (name.startsWith("get") && parameterCount == 0 && name.length() > 3) { name = Introspector.decapitalize(name.substring(3)); this.properties.computeIfAbsent(name, this::getBeanProperty) .addGetter(method); } - else if (name.startsWith("is") && parameterCount == 0) { + else if (name.startsWith("is") && parameterCount == 0 && name.length() > 2) { name = Introspector.decapitalize(name.substring(2)); this.properties.computeIfAbsent(name, this::getBeanProperty) .addGetter(method); } - else if (name.startsWith("set") && parameterCount == 1) { + else if (name.startsWith("set") && parameterCount == 1 && name.length() > 3) { name = Introspector.decapitalize(name.substring(3)); this.properties.computeIfAbsent(name, this::getBeanProperty) .addSetter(method); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java index 8f60f6ac3a..01756c1a96 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/JavaBeanBinderTests.java @@ -476,6 +476,16 @@ public class JavaBeanBinderTests { assertThat(bean.getValue()).isEqualTo(RuntimeException.class); } + @Test + public void bindToClassShouldIgnoreInvalidAccessors() { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo.name", "something"); + this.sources.add(source); + ExampleWithInvalidAccessors bean = this.binder + .bind("foo", Bindable.of(ExampleWithInvalidAccessors.class)).get(); + assertThat(bean.getName()).isEqualTo("something"); + } + public static class ExampleValueBean { private int intValue; @@ -813,6 +823,28 @@ public class JavaBeanBinderTests { } + public static class ExampleWithInvalidAccessors { + + private String name; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String get() { + throw new IllegalArgumentException("should not be invoked"); + } + + public boolean is() { + throw new IllegalArgumentException("should not be invoked"); + } + + } + public enum ExampleEnum { FOO_BAR,