|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
31 | 31 | import javax.validation.MessageInterpolator;
|
32 | 32 | import javax.validation.TraversableResolver;
|
33 | 33 | import javax.validation.Validation;
|
| 34 | +import javax.validation.ValidationProviderResolver; |
34 | 35 | import javax.validation.Validator;
|
35 | 36 | import javax.validation.ValidatorContext;
|
36 | 37 | import javax.validation.ValidatorFactory;
|
| 38 | +import javax.validation.bootstrap.GenericBootstrap; |
| 39 | +import javax.validation.bootstrap.ProviderSpecificBootstrap; |
37 | 40 |
|
38 | 41 | import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
|
39 | 42 |
|
|
51 | 54 | import org.springframework.util.ReflectionUtils;
|
52 | 55 |
|
53 | 56 | /**
|
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 |
57 | 60 | * as well as through the JSR-303 {@link javax.validation.Validator} interface and the
|
58 | 61 | * {@link javax.validation.ValidatorFactory} interface itself.
|
59 | 62 | *
|
@@ -92,6 +95,8 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
|
92 | 95 | @SuppressWarnings("rawtypes")
|
93 | 96 | private Class providerClass;
|
94 | 97 |
|
| 98 | + private ValidationProviderResolver validationProviderResolver; |
| 99 | + |
95 | 100 | private MessageInterpolator messageInterpolator;
|
96 | 101 |
|
97 | 102 | private TraversableResolver traversableResolver;
|
@@ -120,6 +125,15 @@ public void setProviderClass(Class providerClass) {
|
120 | 125 | this.providerClass = providerClass;
|
121 | 126 | }
|
122 | 127 |
|
| 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 | + |
123 | 137 | /**
|
124 | 138 | * Specify a custom MessageInterpolator to use for this ValidatorFactory
|
125 | 139 | * and its exposed default Validator.
|
@@ -216,11 +230,23 @@ public void setApplicationContext(ApplicationContext applicationContext) {
|
216 | 230 |
|
217 | 231 |
|
218 | 232 | @Override
|
| 233 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
219 | 234 | 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 | + } |
224 | 250 |
|
225 | 251 | // Try Hibernate Validator 5.2's externalClassLoader(ClassLoader) method
|
226 | 252 | if (this.applicationContext != null) {
|
|
0 commit comments