1818
1919import jakarta .validation .ConstraintValidator ;
2020import jakarta .validation .ConstraintValidatorFactory ;
21+ import org .jspecify .annotations .Nullable ;
2122
2223import org .springframework .beans .factory .config .AutowireCapableBeanFactory ;
2324import org .springframework .util .Assert ;
@@ -40,6 +41,8 @@ public class SpringConstraintValidatorFactory implements ConstraintValidatorFact
4041
4142 private final AutowireCapableBeanFactory beanFactory ;
4243
44+ private final @ Nullable ConstraintValidatorFactory defaultConstraintValidatorFactory ;
45+
4346
4447 /**
4548 * Create a new SpringConstraintValidatorFactory for the given BeanFactory.
@@ -48,11 +51,35 @@ public class SpringConstraintValidatorFactory implements ConstraintValidatorFact
4851 public SpringConstraintValidatorFactory (AutowireCapableBeanFactory beanFactory ) {
4952 Assert .notNull (beanFactory , "BeanFactory must not be null" );
5053 this .beanFactory = beanFactory ;
54+ this .defaultConstraintValidatorFactory = null ;
55+ }
56+
57+ /**
58+ * Create a new SpringConstraintValidatorFactory for the given BeanFactory.
59+ * @param beanFactory the target BeanFactory
60+ * @param defaultConstraintValidatorFactory the default ConstraintValidatorFactory
61+ * as exposed by the validation provider (for creating provider-internal validator
62+ * implementations which might not be publicly accessible in a module path setup)
63+ * @since 7.0.3
64+ */
65+ public SpringConstraintValidatorFactory (
66+ AutowireCapableBeanFactory beanFactory , ConstraintValidatorFactory defaultConstraintValidatorFactory ) {
67+
68+ Assert .notNull (beanFactory , "BeanFactory must not be null" );
69+ this .beanFactory = beanFactory ;
70+ this .defaultConstraintValidatorFactory = defaultConstraintValidatorFactory ;
5171 }
5272
5373
5474 @ Override
5575 public <T extends ConstraintValidator <?, ?>> T getInstance (Class <T > key ) {
76+ if (this .defaultConstraintValidatorFactory != null ) {
77+ // Create provider-internal validator implementations through default ConstraintValidatorFactory.
78+ String providerModuleName = this .defaultConstraintValidatorFactory .getClass ().getModule ().getName ();
79+ if (providerModuleName != null && providerModuleName .equals (key .getModule ().getName ())) {
80+ return this .defaultConstraintValidatorFactory .getInstance (key );
81+ }
82+ }
5683 return this .beanFactory .createBean (key );
5784 }
5885
0 commit comments