spring4 hibernate validation

从哪里加载的validator呢

默认就是用下面的这个validator
/**
* {@link LocalValidatorFactoryBean} subclass that simply turns
* {@link org.springframework.validation.Validator} calls into no-ops
* in case of no Bean Validation provider being available.
*
* <p>This is the actual class used by Spring's MVC configuration namespace,
* in case of the {@code javax.validation} API being present but no explicit
* Validator having been configured.
*
* @author Juergen Hoeller
* @since 4.0.1
*/
public class OptionalValidatorFactoryBean extends LocalValidatorFactoryBean {

@Override
public void afterPropertiesSet() {
try {
super.afterPropertiesSet();
}
catch (ValidationException ex) {
LogFactory.getLog(getClass()).debug("Failed to set up a Bean Validation provider", ex);
}
}

}


是在WebMvcConfigurationSupport中决定的
[quote] /**
* 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 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.instantiate(clazz);
}
else {
validator = new NoOpValidator();
}
}
return validator;
}[/quote]


这个地方开始进入验证
ModelAttributeMethodProcessor:
1.resolveArgument
{
这个地方有创建binder
WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);

}
this.initializer.initBinder(dataBinder, webRequest);
这句话最重要
/**
* Create a new {@link WebDataBinder} for the given target object and
* initialize it through a {@link WebBindingInitializer}.
* @throws Exception in case of invalid state or arguments
*/
@Override
public final WebDataBinder createBinder(NativeWebRequest webRequest, Object target, String objectName)
throws Exception {
WebDataBinder dataBinder = createBinderInstance(target, objectName, webRequest);
if (this.initializer != null) {
this.initializer.initBinder(dataBinder, webRequest);
}
initBinder(dataBinder, webRequest);
return dataBinder;
}
这个地方有初始化
ConfigurableWebBindingInitializer:initBinder

2.validateIfApplicable

DataBinder: validate
DataBinder中含有
private final List<Validator> validators = new ArrayList<Validator>();
真实的验证发生在
SpringValidatorAdapter 中targetValidator的validate
他的实现可以是hibernate中的ValidatorImpl




public Validator getValidator() {
ResourceBundleLocator defaultResourceBundleLocator = new PlatformResourceBundleLocator(VALIDATION_FILE_NAME);
MessageInterpolator defaultMessageInterpolator = new ResourceBundleMessageInterpolator(
defaultResourceBundleLocator);
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setMessageInterpolator(defaultMessageInterpolator);
bean.setProviderClass(HibernateValidator.class);
// bean.setValidationMessageSource(messageSource());
return bean;
}


上面的可以使用hibernate默认的错误信息
同时包含自定义的错误信息
注意VALIDATION_FILE_NAME不能包含classpath
可以看可以有文件夹
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值