21
21
import java .util .Set ;
22
22
23
23
import jakarta .validation .ConstraintValidator ;
24
+ import jakarta .validation .NoProviderFoundException ;
24
25
import jakarta .validation .Validation ;
25
26
import jakarta .validation .Validator ;
26
27
import jakarta .validation .metadata .BeanDescriptor ;
48
49
* required for {@link ConstraintValidator}s.
49
50
*
50
51
* @author Sebastien Deleuze
52
+ * @author Juergen Hoeller
51
53
* @since 6.0.5
52
54
*/
53
55
class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@@ -57,21 +59,39 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
57
59
58
60
private static final Log logger = LogFactory .getLog (BeanValidationBeanRegistrationAotProcessor .class );
59
61
60
- @ Nullable
62
+
61
63
@ Override
64
+ @ Nullable
62
65
public BeanRegistrationAotContribution processAheadOfTime (RegisteredBean registeredBean ) {
63
66
if (isBeanValidationPresent ) {
64
67
return BeanValidationDelegate .processAheadOfTime (registeredBean );
65
68
}
66
69
return null ;
67
70
}
68
71
72
+
69
73
private static class BeanValidationDelegate {
70
74
71
- private static final Validator validator = Validation .buildDefaultValidatorFactory ().getValidator ();
75
+ @ Nullable
76
+ private static final Validator validator = getValidatorIfAvailable ();
77
+
78
+ @ Nullable
79
+ private static Validator getValidatorIfAvailable () {
80
+ try {
81
+ return Validation .buildDefaultValidatorFactory ().getValidator ();
82
+ }
83
+ catch (NoProviderFoundException ex ) {
84
+ logger .info ("No Bean Validation provider available - skipping validation constraint hint inference" );
85
+ return null ;
86
+ }
87
+ }
72
88
73
89
@ Nullable
74
90
public static BeanRegistrationAotContribution processAheadOfTime (RegisteredBean registeredBean ) {
91
+ if (validator == null ) {
92
+ return null ;
93
+ }
94
+
75
95
BeanDescriptor descriptor ;
76
96
try {
77
97
descriptor = validator .getConstraintsForClass (registeredBean .getBeanClass ());
@@ -88,6 +108,7 @@ public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean
88
108
}
89
109
return null ;
90
110
}
111
+
91
112
Set <ConstraintDescriptor <?>> constraintDescriptors = new HashSet <>();
92
113
for (MethodDescriptor methodDescriptor : descriptor .getConstrainedMethods (MethodType .NON_GETTER , MethodType .GETTER )) {
93
114
for (ParameterDescriptor parameterDescriptor : methodDescriptor .getParameterDescriptors ()) {
@@ -107,9 +128,9 @@ public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean
107
128
}
108
129
return null ;
109
130
}
110
-
111
131
}
112
132
133
+
113
134
private static class BeanValidationBeanRegistrationAotContribution implements BeanRegistrationAotContribution {
114
135
115
136
private final Collection <ConstraintDescriptor <?>> constraintDescriptors ;
0 commit comments