mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Use default ConstraintValidatorFactory for provider-internal validators
Closes gh-36012
This commit is contained in:
+3
-2
@@ -285,8 +285,9 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
|
||||
|
||||
ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
|
||||
if (targetConstraintValidatorFactory == null && this.applicationContext != null) {
|
||||
targetConstraintValidatorFactory =
|
||||
new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
|
||||
targetConstraintValidatorFactory = new SpringConstraintValidatorFactory(
|
||||
this.applicationContext.getAutowireCapableBeanFactory(),
|
||||
configuration.getDefaultConstraintValidatorFactory());
|
||||
}
|
||||
if (targetConstraintValidatorFactory != null) {
|
||||
configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
|
||||
|
||||
+27
@@ -18,6 +18,7 @@ package org.springframework.validation.beanvalidation;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorFactory;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -40,6 +41,8 @@ public class SpringConstraintValidatorFactory implements ConstraintValidatorFact
|
||||
|
||||
private final AutowireCapableBeanFactory beanFactory;
|
||||
|
||||
private final @Nullable ConstraintValidatorFactory defaultConstraintValidatorFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new SpringConstraintValidatorFactory for the given BeanFactory.
|
||||
@@ -48,11 +51,35 @@ public class SpringConstraintValidatorFactory implements ConstraintValidatorFact
|
||||
public SpringConstraintValidatorFactory(AutowireCapableBeanFactory beanFactory) {
|
||||
Assert.notNull(beanFactory, "BeanFactory must not be null");
|
||||
this.beanFactory = beanFactory;
|
||||
this.defaultConstraintValidatorFactory = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new SpringConstraintValidatorFactory for the given BeanFactory.
|
||||
* @param beanFactory the target BeanFactory
|
||||
* @param defaultConstraintValidatorFactory the default ConstraintValidatorFactory
|
||||
* as exposed by the validation provider (for creating provider-internal validator
|
||||
* implementations which might not be publicly accessible in a module path setup)
|
||||
* @since 7.0.3
|
||||
*/
|
||||
public SpringConstraintValidatorFactory(
|
||||
AutowireCapableBeanFactory beanFactory, ConstraintValidatorFactory defaultConstraintValidatorFactory) {
|
||||
|
||||
Assert.notNull(beanFactory, "BeanFactory must not be null");
|
||||
this.beanFactory = beanFactory;
|
||||
this.defaultConstraintValidatorFactory = defaultConstraintValidatorFactory;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
|
||||
if (this.defaultConstraintValidatorFactory != null) {
|
||||
// Create provider-internal validator implementations through default ConstraintValidatorFactory.
|
||||
String providerModuleName = this.defaultConstraintValidatorFactory.getClass().getModule().getName();
|
||||
if (providerModuleName != null && providerModuleName.equals(key.getModule().getName())) {
|
||||
return this.defaultConstraintValidatorFactory.getInstance(key);
|
||||
}
|
||||
}
|
||||
return this.beanFactory.createBean(key);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user