use of org.springframework.validation.Validator in project spring-framework by spring-projects.
the class WebFluxConfigurationSupport method webFluxValidator.
/**
* Return a global {@link Validator} instance for example for validating
* {@code @RequestBody} method arguments.
* <p>Delegates to {@link #getValidator()} first. If that returns {@code null}
* checks the classpath for the presence of a JSR-303 implementations
* before creating a {@code OptionalValidatorFactoryBean}. If a JSR-303
* implementation is not available, a "no-op" {@link Validator} is returned.
*/
@Bean
public Validator webFluxValidator() {
Validator validator = getValidator();
if (validator == null) {
if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
Class<?> clazz;
try {
String name = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
clazz = ClassUtils.forName(name, getClass().getClassLoader());
} catch (ClassNotFoundException ex) {
throw new BeanInitializationException("Could not find default validator class", ex);
} catch (LinkageError ex) {
throw new BeanInitializationException("Could not load default validator class", ex);
}
validator = (Validator) BeanUtils.instantiateClass(clazz);
} else {
validator = new NoOpValidator();
}
}
return validator;
}
use of org.springframework.validation.Validator in project spring-framework by spring-projects.
the class WebMvcConfigurationSupport method mvcValidator.
/**
* Return a global {@link Validator} instance for example for validating
* {@code @ModelAttribute} and {@code @RequestBody} method arguments.
* Delegates to {@link #getValidator()} first and if that returns {@code null}
* checks the classpath for the presence of a JSR-303 implementations
* before creating a {@code OptionalValidatorFactoryBean}.If a JSR-303
* implementation is not available, a no-op {@link Validator} is returned.
*/
@Bean
public Validator mvcValidator() {
Validator validator = getValidator();
if (validator == null) {
if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
Class<?> clazz;
try {
String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
} catch (ClassNotFoundException | LinkageError ex) {
throw new BeanInitializationException("Could not find default validator class", ex);
}
validator = (Validator) BeanUtils.instantiateClass(clazz);
} else {
validator = new NoOpValidator();
}
}
return validator;
}
use of org.springframework.validation.Validator in project spring-boot by spring-projects.
the class ConfigurationPropertiesBindingPostProcessor method determineValidator.
private Validator determineValidator(Object bean) {
Validator validator = getValidator();
boolean supportsBean = (validator != null && validator.supports(bean.getClass()));
if (ClassUtils.isAssignable(Validator.class, bean.getClass())) {
if (supportsBean) {
return new ChainingValidator(validator, (Validator) bean);
}
return (Validator) bean;
}
return (supportsBean ? validator : null);
}
use of org.springframework.validation.Validator in project spring-boot by spring-projects.
the class ConfigurationPropertiesBindingPostProcessor method freeLocalValidator.
private void freeLocalValidator() {
try {
Validator validator = this.localValidator;
this.localValidator = null;
if (validator != null) {
((DisposableBean) validator).destroy();
}
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
use of org.springframework.validation.Validator in project spring-boot by spring-projects.
the class WebFluxAnnotationAutoConfigurationTests method validationJsr303CustomValidatorReusedAsSpringValidator.
@Test
public void validationJsr303CustomValidatorReusedAsSpringValidator() {
load(CustomValidator.class);
assertThat(this.context.getBeansOfType(ValidatorFactory.class)).hasSize(1);
assertThat(this.context.getBeansOfType(javax.validation.Validator.class)).hasSize(1);
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(2);
Validator validator = this.context.getBean("webFluxValidator", Validator.class);
assertThat(validator).isInstanceOf(SpringValidator.class);
assertThat(((SpringValidator) validator).getTarget()).isSameAs(this.context.getBean(javax.validation.Validator.class));
}
Aggregations