Browse Source

LocalValidatorFactoryBean supports custom ValidationProviderResolver

Issue: SPR-14100
pull/1019/merge
Juergen Hoeller 9 years ago
parent
commit
739ae8c045
  1. 42
      spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

42
spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,9 +31,12 @@ import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator; import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver; import javax.validation.TraversableResolver;
import javax.validation.Validation; import javax.validation.Validation;
import javax.validation.ValidationProviderResolver;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.ValidatorContext; import javax.validation.ValidatorContext;
import javax.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
import javax.validation.bootstrap.GenericBootstrap;
import javax.validation.bootstrap.ProviderSpecificBootstrap;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator; import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
@ -51,9 +54,9 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
* This is the central class for {@code javax.validation} (JSR-303) setup * This is the central class for {@code javax.validation} (JSR-303) setup in a Spring
* in a Spring application context: It bootstraps a {@code javax.validation.ValidationFactory} * application context: It bootstraps a {@code javax.validation.ValidationFactory} and
* and exposes it through the Spring {@link org.springframework.validation.Validator} interface * exposes it through the Spring {@link org.springframework.validation.Validator} interface
* as well as through the JSR-303 {@link javax.validation.Validator} interface and the * as well as through the JSR-303 {@link javax.validation.Validator} interface and the
* {@link javax.validation.ValidatorFactory} interface itself. * {@link javax.validation.ValidatorFactory} interface itself.
* *
@ -92,6 +95,8 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private Class providerClass; private Class providerClass;
private ValidationProviderResolver validationProviderResolver;
private MessageInterpolator messageInterpolator; private MessageInterpolator messageInterpolator;
private TraversableResolver traversableResolver; private TraversableResolver traversableResolver;
@ -120,6 +125,15 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
this.providerClass = providerClass; this.providerClass = providerClass;
} }
/**
* Specify a JSR-303 {@link ValidationProviderResolver} for bootstrapping the
* provider of choice, as an alternative to {@code META-INF} driven resolution.
* @since 4.3
*/
public void setValidationProviderResolver(ValidationProviderResolver validationProviderResolver) {
this.validationProviderResolver = validationProviderResolver;
}
/** /**
* Specify a custom MessageInterpolator to use for this ValidatorFactory * Specify a custom MessageInterpolator to use for this ValidatorFactory
* and its exposed default Validator. * and its exposed default Validator.
@ -216,11 +230,23 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
@Override @Override
@SuppressWarnings({"rawtypes", "unchecked"})
public void afterPropertiesSet() { public void afterPropertiesSet() {
@SuppressWarnings({"rawtypes", "unchecked"}) Configuration<?> configuration;
Configuration<?> configuration = (this.providerClass != null ? if (this.providerClass != null) {
Validation.byProvider(this.providerClass).configure() : ProviderSpecificBootstrap bootstrap = Validation.byProvider(this.providerClass);
Validation.byDefaultProvider().configure()); if (this.validationProviderResolver != null) {
bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
}
configuration = bootstrap.configure();
}
else {
GenericBootstrap bootstrap = Validation.byDefaultProvider();
if (this.validationProviderResolver != null) {
bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
}
configuration = bootstrap.configure();
}
// Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method // Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
if (this.applicationContext != null) { if (this.applicationContext != null) {

Loading…
Cancel
Save