Improve @ConfigurationProperty binding exception

Include more details in the message including the class being bound
and the prefix from the annotation.

Fixes gh-2246
pull/2300/head
Phillip Webb 10 years ago
parent 0fc89ddb20
commit 165b85dd0e

@ -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) {

@ -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;

Loading…
Cancel
Save