Merge branch '1.5.x'
This commit is contained in:
@@ -65,8 +65,6 @@ public class PropertiesConfigurationFactory<T>
|
||||
|
||||
private boolean ignoreInvalidFields;
|
||||
|
||||
private boolean exceptionIfInvalid = true;
|
||||
|
||||
private PropertySources propertySources;
|
||||
|
||||
private final T target;
|
||||
@@ -182,15 +180,6 @@ public class PropertiesConfigurationFactory<T>
|
||||
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<T>
|
||||
|
||||
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<T>
|
||||
Locale.getDefault()) + " (" + error + ")"
|
||||
: error);
|
||||
}
|
||||
if (this.exceptionIfInvalid) {
|
||||
throw new BindException(errors);
|
||||
}
|
||||
throw new BindException(errors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<T>
|
||||
|
||||
private final Class<?> type;
|
||||
|
||||
private boolean exceptionIfInvalid;
|
||||
|
||||
private String yaml;
|
||||
|
||||
private Resource resource;
|
||||
@@ -122,10 +119,6 @@ public class YamlConfigurationFactory<T>
|
||||
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<T>
|
||||
}
|
||||
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<T>
|
||||
for (ObjectError error : errors.getAllErrors()) {
|
||||
logger.error(getErrorMessage(error));
|
||||
}
|
||||
if (this.exceptionIfInvalid) {
|
||||
BindException summary = new BindException(errors);
|
||||
throw summary;
|
||||
}
|
||||
throw new BindException(errors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -48,7 +48,6 @@ public class YamlConfigurationFactoryTests {
|
||||
YamlConfigurationFactory<Foo> factory = new YamlConfigurationFactory<Foo>(
|
||||
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<Jee> factory = new YamlConfigurationFactory<Jee>(
|
||||
Jee.class);
|
||||
factory.setYaml(yaml);
|
||||
factory.setExceptionIfInvalid(true);
|
||||
factory.setPropertyAliases(this.aliases);
|
||||
factory.setValidator(this.validator);
|
||||
factory.setMessageSource(new StaticMessageSource());
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user