Skip to content

Commit 739ae8c

Browse files
committed
LocalValidatorFactoryBean supports custom ValidationProviderResolver
Issue: SPR-14100
1 parent 6826bdd commit 739ae8c

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,9 +31,12 @@
3131
import javax.validation.MessageInterpolator;
3232
import javax.validation.TraversableResolver;
3333
import javax.validation.Validation;
34+
import javax.validation.ValidationProviderResolver;
3435
import javax.validation.Validator;
3536
import javax.validation.ValidatorContext;
3637
import javax.validation.ValidatorFactory;
38+
import javax.validation.bootstrap.GenericBootstrap;
39+
import javax.validation.bootstrap.ProviderSpecificBootstrap;
3740

3841
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
3942

@@ -51,9 +54,9 @@
5154
import org.springframework.util.ReflectionUtils;
5255

5356
/**
54-
* This is the central class for {@code javax.validation} (JSR-303) setup
55-
* in a Spring application context: It bootstraps a {@code javax.validation.ValidationFactory}
56-
* and exposes it through the Spring {@link org.springframework.validation.Validator} interface
57+
* This is the central class for {@code javax.validation} (JSR-303) setup in a Spring
58+
* application context: It bootstraps a {@code javax.validation.ValidationFactory} and
59+
* exposes it through the Spring {@link org.springframework.validation.Validator} interface
5760
* as well as through the JSR-303 {@link javax.validation.Validator} interface and the
5861
* {@link javax.validation.ValidatorFactory} interface itself.
5962
*
@@ -92,6 +95,8 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
9295
@SuppressWarnings("rawtypes")
9396
private Class providerClass;
9497

98+
private ValidationProviderResolver validationProviderResolver;
99+
95100
private MessageInterpolator messageInterpolator;
96101

97102
private TraversableResolver traversableResolver;
@@ -120,6 +125,15 @@ public void setProviderClass(Class providerClass) {
120125
this.providerClass = providerClass;
121126
}
122127

128+
/**
129+
* Specify a JSR-303 {@link ValidationProviderResolver} for bootstrapping the
130+
* provider of choice, as an alternative to {@code META-INF} driven resolution.
131+
* @since 4.3
132+
*/
133+
public void setValidationProviderResolver(ValidationProviderResolver validationProviderResolver) {
134+
this.validationProviderResolver = validationProviderResolver;
135+
}
136+
123137
/**
124138
* Specify a custom MessageInterpolator to use for this ValidatorFactory
125139
* and its exposed default Validator.
@@ -216,11 +230,23 @@ public void setApplicationContext(ApplicationContext applicationContext) {
216230

217231

218232
@Override
233+
@SuppressWarnings({"rawtypes", "unchecked"})
219234
public void afterPropertiesSet() {
220-
@SuppressWarnings({"rawtypes", "unchecked"})
221-
Configuration<?> configuration = (this.providerClass != null ?
222-
Validation.byProvider(this.providerClass).configure() :
223-
Validation.byDefaultProvider().configure());
235+
Configuration<?> configuration;
236+
if (this.providerClass != null) {
237+
ProviderSpecificBootstrap bootstrap = Validation.byProvider(this.providerClass);
238+
if (this.validationProviderResolver != null) {
239+
bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
240+
}
241+
configuration = bootstrap.configure();
242+
}
243+
else {
244+
GenericBootstrap bootstrap = Validation.byDefaultProvider();
245+
if (this.validationProviderResolver != null) {
246+
bootstrap = bootstrap.providerResolver(this.validationProviderResolver);
247+
}
248+
configuration = bootstrap.configure();
249+
}
224250

225251
// Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
226252
if (this.applicationContext != null) {

0 commit comments

Comments
 (0)