diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java index 4e8a368c90c9..05d39d12c322 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java @@ -21,6 +21,7 @@ import java.util.Set; import jakarta.validation.ConstraintValidator; +import jakarta.validation.NoProviderFoundException; import jakarta.validation.Validation; import jakarta.validation.Validator; import jakarta.validation.metadata.BeanDescriptor; @@ -48,6 +49,7 @@ * required for {@link ConstraintValidator}s. * * @author Sebastien Deleuze + * @author Juergen Hoeller * @since 6.0.5 */ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { @@ -57,8 +59,9 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP private static final Log logger = LogFactory.getLog(BeanValidationBeanRegistrationAotProcessor.class); - @Nullable + @Override + @Nullable public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { if (isBeanValidationPresent) { return BeanValidationDelegate.processAheadOfTime(registeredBean); @@ -66,12 +69,29 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe return null; } + private static class BeanValidationDelegate { - private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); + @Nullable + private static final Validator validator = getValidatorIfAvailable(); + + @Nullable + private static Validator getValidatorIfAvailable() { + try { + return Validation.buildDefaultValidatorFactory().getValidator(); + } + catch (NoProviderFoundException ex) { + logger.info("No Bean Validation provider available - skipping validation constraint hint inference"); + return null; + } + } @Nullable public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { + if (validator == null) { + return null; + } + BeanDescriptor descriptor; try { descriptor = validator.getConstraintsForClass(registeredBean.getBeanClass()); @@ -88,6 +108,7 @@ public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean } return null; } + Set> constraintDescriptors = new HashSet<>(); for (MethodDescriptor methodDescriptor : descriptor.getConstrainedMethods(MethodType.NON_GETTER, MethodType.GETTER)) { for (ParameterDescriptor parameterDescriptor : methodDescriptor.getParameterDescriptors()) { @@ -107,9 +128,9 @@ public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean } return null; } - } + private static class BeanValidationBeanRegistrationAotContribution implements BeanRegistrationAotContribution { private final Collection> constraintDescriptors;