diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java index dbff019a65..68212b6d24 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java @@ -65,8 +65,6 @@ public class PropertiesConfigurationFactory private boolean ignoreInvalidFields; - private boolean exceptionIfInvalid = true; - private PropertySources propertySources; private final T target; @@ -182,15 +180,6 @@ public class PropertiesConfigurationFactory this.validator = validator; } - /** - * Set a flag to indicate that an exception should be raised if a Validator is - * available and validation fails. - * @param exceptionIfInvalid the flag to set - */ - public void setExceptionIfInvalid(boolean exceptionIfInvalid) { - this.exceptionIfInvalid = exceptionIfInvalid; - } - /** * Flag to indicate that placeholders should be replaced during binding. Default is * true. @@ -228,22 +217,12 @@ public class PropertiesConfigurationFactory public void bindPropertiesToTarget() throws BindException { Assert.state(this.propertySources != null, "PropertySources should not be null"); - try { - if (logger.isTraceEnabled()) { - logger.trace("Property Sources: " + this.propertySources); + if (logger.isTraceEnabled()) { + logger.trace("Property Sources: " + this.propertySources); - } - this.hasBeenBound = true; - doBindPropertiesToTarget(); - } - catch (BindException ex) { - if (this.exceptionIfInvalid) { - throw ex; - } - PropertiesConfigurationFactory.logger - .error("Failed to load Properties validation bean. " - + "Your Properties may be invalid.", ex); } + this.hasBeenBound = true; + doBindPropertiesToTarget(); } private void doBindPropertiesToTarget() throws BindException { @@ -351,9 +330,7 @@ public class PropertiesConfigurationFactory Locale.getDefault()) + " (" + error + ")" : error); } - if (this.exceptionIfInvalid) { - throw new BindException(errors); - } + throw new BindException(errors); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java index fd45de3296..8311e96d3f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; -import org.yaml.snakeyaml.error.YAMLException; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; @@ -56,8 +55,6 @@ public class YamlConfigurationFactory private final Class type; - private boolean exceptionIfInvalid; - private String yaml; private Resource resource; @@ -122,10 +119,6 @@ public class YamlConfigurationFactory this.validator = validator; } - public void setExceptionIfInvalid(boolean exceptionIfInvalid) { - this.exceptionIfInvalid = exceptionIfInvalid; - } - @Override @SuppressWarnings("unchecked") public void afterPropertiesSet() throws Exception { @@ -136,23 +129,14 @@ public class YamlConfigurationFactory } Assert.state(this.yaml != null, "Yaml document should not be null: " + "either set it directly or set the resource to load it from"); - try { - if (logger.isTraceEnabled()) { - logger.trace(String.format("Yaml document is %n%s", this.yaml)); - } - Constructor constructor = new YamlJavaBeanPropertyConstructor(this.type, - this.propertyAliases); - this.configuration = (T) (new Yaml(constructor)).load(this.yaml); - if (this.validator != null) { - validate(); - } + if (logger.isTraceEnabled()) { + logger.trace(String.format("Yaml document is %n%s", this.yaml)); } - catch (YAMLException ex) { - if (this.exceptionIfInvalid) { - throw ex; - } - logger.error("Failed to load YAML validation bean. " - + "Your YAML file may be invalid.", ex); + Constructor constructor = new YamlJavaBeanPropertyConstructor(this.type, + this.propertyAliases); + this.configuration = (T) (new Yaml(constructor)).load(this.yaml); + if (this.validator != null) { + validate(); } } @@ -165,10 +149,7 @@ public class YamlConfigurationFactory for (ObjectError error : errors.getAllErrors()) { logger.error(getErrorMessage(error)); } - if (this.exceptionIfInvalid) { - BindException summary = new BindException(errors); - throw summary; - } + throw new BindException(errors); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java index 683c3ad8d3..9c3f7da6e5 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java @@ -23,7 +23,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor; -import org.springframework.validation.annotation.Validated; /** * Annotation for externalized configuration. Add this to a class definition or a @@ -80,13 +79,4 @@ public @interface ConfigurationProperties { */ boolean ignoreUnknownFields() default true; - /** - * Flag to indicate that an exception should be raised if a Validator is available, - * the class is annotated with {@link Validated @Validated} and validation fails. If - * it is set to false, validation errors will be swallowed. They will be logged, but - * not propagated to the caller. - * @return the flag value (default true) - */ - boolean exceptionIfInvalid() default true; - } 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 a75bbb58ae..5d3c5166ba 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 @@ -304,6 +304,7 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc return bean; } + @SuppressWarnings("deprecation") private void postProcessBeforeInitialization(Object bean, String beanName, ConfigurationProperties annotation) { Object target = bean; @@ -318,7 +319,6 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc if (annotation != null) { factory.setIgnoreInvalidFields(annotation.ignoreInvalidFields()); factory.setIgnoreUnknownFields(annotation.ignoreUnknownFields()); - factory.setExceptionIfInvalid(annotation.exceptionIfInvalid()); factory.setIgnoreNestedProperties(annotation.ignoreNestedProperties()); if (StringUtils.hasLength(annotation.prefix())) { factory.setTargetName(annotation.prefix()); diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java index b5da56ca2d..785ae93acf 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,15 +83,6 @@ public class PropertiesConfigurationFactoryTests { createFoo("bar: blah"); } - @Test - public void testValidationErrorCanBeSuppressed() throws Exception { - this.validator = new SpringValidatorAdapter( - Validation.buildDefaultValidatorFactory().getValidator()); - setupFactory(); - this.factory.setExceptionIfInvalid(false); - bindFoo("bar: blah"); - } - @Test public void systemEnvironmentBindingFailuresAreIgnored() throws Exception { setupFactory(); diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/YamlConfigurationFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/YamlConfigurationFactoryTests.java index 36ee414ab6..ed1fbf3512 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/YamlConfigurationFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/YamlConfigurationFactoryTests.java @@ -48,7 +48,6 @@ public class YamlConfigurationFactoryTests { YamlConfigurationFactory factory = new YamlConfigurationFactory( Foo.class); factory.setYaml(yaml); - factory.setExceptionIfInvalid(true); factory.setPropertyAliases(this.aliases); factory.setValidator(this.validator); factory.setMessageSource(new StaticMessageSource()); @@ -60,7 +59,6 @@ public class YamlConfigurationFactoryTests { YamlConfigurationFactory factory = new YamlConfigurationFactory( Jee.class); factory.setYaml(yaml); - factory.setExceptionIfInvalid(true); factory.setPropertyAliases(this.aliases); factory.setValidator(this.validator); factory.setMessageSource(new StaticMessageSource()); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java index 5dd52c46cc..c69ac5bc8c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesTests.java @@ -184,18 +184,6 @@ public class EnableConfigurationPropertiesTests { assertThat(bean.getDescription()).isNull(); } - @Test - public void testNoExceptionOnValidation() { - this.context.register(NoExceptionIfInvalidTestConfiguration.class); - TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, - "name=foo"); - this.context.refresh(); - assertThat(this.context - .getBeanNamesForType(NoExceptionIfInvalidTestProperties.class)) - .hasSize(1); - assertThat(this.context.getBean(TestProperties.class).name).isEqualTo("foo"); - } - @Test public void testNestedPropertiesBinding() { this.context.register(NestedConfiguration.class); @@ -450,12 +438,6 @@ public class EnableConfigurationPropertiesTests { } - @Configuration - @EnableConfigurationProperties(NoExceptionIfInvalidTestProperties.class) - protected static class NoExceptionIfInvalidTestConfiguration { - - } - @Configuration @EnableConfigurationProperties(DerivedProperties.class) protected static class DerivedConfiguration { @@ -709,23 +691,6 @@ public class EnableConfigurationPropertiesTests { } - @ConfigurationProperties(exceptionIfInvalid = false) - @Validated - protected static class NoExceptionIfInvalidTestProperties extends TestProperties { - - @NotNull - private String description; - - public String getDescription() { - return this.description; - } - - public void setDescription(String description) { - this.description = description; - } - - } - @ConfigurationProperties protected static class MoreProperties {