From 165b85dd0e23d71c96d3f07cfe2ccc4885c70ce3 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 6 Jan 2015 16:02:43 -0800 Subject: [PATCH] Improve @ConfigurationProperty binding exception Include more details in the message including the class being bound and the prefix from the annotation. Fixes gh-2246 --- ...urationPropertiesBindingPostProcessor.java | 22 ++++++++++++++++++- ...onPropertiesBindingPostProcessorTests.java | 17 +++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java index d29e09263d..8ce9f32c8f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java @@ -296,8 +296,28 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc factory.bindPropertiesToTarget(); } catch (Exception ex) { - throw new BeanCreationException(beanName, "Could not bind properties", ex); + String targetClass = "[unknown]"; + if (target != null) { + ClassUtils.getShortName(target.getClass()); + } + throw new BeanCreationException(beanName, "Could not bind properties to " + + targetClass + " (" + getAnnotationDetails(annotation) + ")", ex); + } + } + + private String getAnnotationDetails(ConfigurationProperties annotation) { + if (annotation == null) { + return ""; } + StringBuilder details = new StringBuilder(); + details.append("target=").append( + (StringUtils.hasLength(annotation.value()) ? annotation.value() + : annotation.prefix())); + details.append(", ignoreInvalidFields=").append(annotation.ignoreInvalidFields()); + details.append(", ignoreUnknownFields=").append(annotation.ignoreUnknownFields()); + details.append(", ignoreNestedProperties=").append( + annotation.ignoreNestedProperties()); + return details.toString(); } private Validator determineValidator(Object bean) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java index dedf4bf375..debb44a57d 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java @@ -20,7 +20,9 @@ import javax.annotation.PostConstruct; import javax.validation.constraints.NotNull; import org.junit.After; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.FactoryBean; @@ -55,6 +57,9 @@ import static org.junit.Assert.fail; */ public class ConfigurationPropertiesBindingPostProcessorTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private AnnotationConfigApplicationContext context; @After @@ -198,6 +203,16 @@ public class ConfigurationPropertiesBindingPostProcessorTests { equalTo("word".toCharArray())); } + @Test + public void notWritablePropertyException() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, "test.madeup:word"); + this.context.register(PropertyWithCharArray.class); + this.thrown.expect(BeanCreationException.class); + this.thrown.expectMessage("test"); + this.context.refresh(); + } + @Configuration @EnableConfigurationProperties public static class TestConfigurationWithValidatingSetter { @@ -314,7 +329,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests { @Configuration @EnableConfigurationProperties - @ConfigurationProperties(prefix = "test") + @ConfigurationProperties(prefix = "test", ignoreUnknownFields = false) public static class PropertyWithCharArray { private char[] chars;