From cd2b7afc8751804fa97d28bbf95b41fe03347dac Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 8 Aug 2022 16:34:49 +0200 Subject: [PATCH] Avoid reflection on OptionalValidatorFactoryBean Closes gh-28939 --- .../config/AbstractMessageBrokerConfiguration.java | 9 +++------ .../reactive/config/WebFluxConfigurationSupport.java | 11 ++++------- .../config/annotation/WebMvcConfigurationSupport.java | 11 ++++------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java index e136ffd2889b..4615a7ed5ad8 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Map; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; @@ -71,6 +70,7 @@ import org.springframework.util.StringUtils; import org.springframework.validation.Errors; import org.springframework.validation.Validator; +import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; /** * Provides essential configuration for handling messages with simple messaging @@ -550,15 +550,12 @@ protected Validator simpValidator() { validator = this.applicationContext.getBean(MVC_VALIDATOR_NAME, Validator.class); } else if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) { - Class clazz; try { - String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean"; - clazz = ClassUtils.forName(className, AbstractMessageBrokerConfiguration.class.getClassLoader()); + validator = new OptionalValidatorFactoryBean(); } catch (Throwable ex) { - throw new BeanInitializationException("Could not find default validator class", ex); + throw new BeanInitializationException("Failed to create default validator", ex); } - validator = (Validator) BeanUtils.instantiateClass(clazz); } else { validator = new Validator() { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java index 627e40353037..a90b9ced4b0e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java @@ -22,7 +22,6 @@ import reactor.core.publisher.Mono; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; @@ -45,6 +44,7 @@ import org.springframework.validation.Errors; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; +import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.WebAnnotationsRuntimeHintsRegistrar; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; @@ -388,15 +388,12 @@ public Validator webFluxValidator() { Validator validator = getValidator(); if (validator == null) { if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) { - Class clazz; try { - String name = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean"; - clazz = ClassUtils.forName(name, getClass().getClassLoader()); + validator = new OptionalValidatorFactoryBean(); } - catch (ClassNotFoundException | LinkageError ex) { - throw new BeanInitializationException("Failed to resolve default validator class", ex); + catch (Throwable ex) { + throw new BeanInitializationException("Failed to create default validator", ex); } - validator = (Validator) BeanUtils.instantiateClass(clazz); } else { validator = new NoOpValidator(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 6f6cc56c0689..eda055e562e6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -25,7 +25,6 @@ import jakarta.servlet.ServletContext; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.annotation.Qualifier; @@ -68,6 +67,7 @@ import org.springframework.validation.Errors; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; +import org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean; import org.springframework.web.HttpRequestHandler; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.bind.WebDataBinder; @@ -760,15 +760,12 @@ public Validator mvcValidator() { Validator validator = getValidator(); if (validator == null) { if (ClassUtils.isPresent("jakarta.validation.Validator", getClass().getClassLoader())) { - Class clazz; try { - String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean"; - clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader()); + validator = new OptionalValidatorFactoryBean(); } - catch (ClassNotFoundException | LinkageError ex) { - throw new BeanInitializationException("Failed to resolve default validator class", ex); + catch (Throwable ex) { + throw new BeanInitializationException("Failed to create default validator", ex); } - validator = (Validator) BeanUtils.instantiateClass(clazz); } else { validator = new NoOpValidator();