Browse Source

Avoid reflection on OptionalValidatorFactoryBean

Closes gh-28939
pull/28944/head
Stephane Nicoll 3 years ago
parent
commit
cd2b7afc87
  1. 9
      spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java
  2. 11
      spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
  3. 11
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

9
spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

@ -22,7 +22,6 @@ import java.util.HashMap; @@ -22,7 +22,6 @@ import java.util.HashMap;
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.PathMatcher; @@ -71,6 +70,7 @@ import org.springframework.util.PathMatcher;
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 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC @@ -550,15 +550,12 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
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() {

11
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java

@ -22,7 +22,6 @@ import java.util.function.Predicate; @@ -22,7 +22,6 @@ import java.util.function.Predicate;
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.util.ClassUtils; @@ -45,6 +44,7 @@ import org.springframework.util.ClassUtils;
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 class WebFluxConfigurationSupport implements ApplicationContextAware { @@ -388,15 +388,12 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
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();

11
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

@ -25,7 +25,6 @@ import java.util.Map; @@ -25,7 +25,6 @@ import java.util.Map;
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.util.PathMatcher; @@ -68,6 +67,7 @@ import org.springframework.util.PathMatcher;
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 class WebMvcConfigurationSupport implements ApplicationContextAware, Serv @@ -760,15 +760,12 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
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();

Loading…
Cancel
Save