BeanValidationPostProcessor runs in before-initialization phase by default (SPR-6565)
This commit is contained in:
@@ -29,33 +29,77 @@ import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
/**
|
||||
* Simple {@link BeanPostProcessor} that checks JSR-303 constraint annotations
|
||||
* in Spring-managed beans, throwing an initialization exception in case of
|
||||
* constraint violations right before calling the bean's init method (if any).
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class BeanValidationPostProcessor implements BeanPostProcessor, InitializingBean {
|
||||
|
||||
private javax.validation.Validator validator;
|
||||
private Validator validator;
|
||||
|
||||
private boolean afterInitialization = false;
|
||||
|
||||
|
||||
/**
|
||||
* Set the JSR-303 Validator to delegate to for validating beans.
|
||||
* <p>Default is the default ValidatorFactory's default Validator.
|
||||
*/
|
||||
public void setValidator(Validator validator) {
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the JSR-303 ValidatorFactory to delegate to for validating beans,
|
||||
* using its default Validator.
|
||||
* <p>Default is the default ValidatorFactory's default Validator.
|
||||
* @see javax.validation.ValidatorFactory#getValidator()
|
||||
*/
|
||||
public void setValidatorFactory(ValidatorFactory validatorFactory) {
|
||||
this.validator = validatorFactory.getValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Choose whether to perform validation after bean initialization
|
||||
* (i.e. after init methods) instead of before (which is the default).
|
||||
* <p>Default is "false" (before initialization). Switch this to "true"
|
||||
* (after initialization) if you would like to give init methods a chance
|
||||
* to populate constrained fields before they get validated.
|
||||
*/
|
||||
public void setAfterInitialization(boolean afterInitialization) {
|
||||
this.afterInitialization = afterInitialization;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
if (this.validator == null) {
|
||||
Validation.buildDefaultValidatorFactory().getValidator();
|
||||
this.validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (!this.afterInitialization) {
|
||||
doValidate(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (this.afterInitialization) {
|
||||
doValidate(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Perform validation of the given bean.
|
||||
* @param bean the bean instance to validate
|
||||
* @see javax.validation.Validator#validate
|
||||
*/
|
||||
protected void doValidate(Object bean) {
|
||||
Set<ConstraintViolation<Object>> result = this.validator.validate(bean);
|
||||
if (!result.isEmpty()) {
|
||||
StringBuilder sb = new StringBuilder("Bean state is invalid: ");
|
||||
@@ -68,7 +112,6 @@ public class BeanValidationPostProcessor implements BeanPostProcessor, Initializ
|
||||
}
|
||||
throw new BeanInitializationException(sb.toString());
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user