Skip to content

Commit 1aa1278

Browse files
committed
SPR-8725 Change modifier in WebMvcConfigurationSupport methods from package private to public.
Use of package private @bean methods can cause issues if the class is extended and the sub-class is in a different package. This is covered in detail in SPR-8756.
1 parent 9be6ddc commit 1aa1278

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -403,37 +403,40 @@ protected void addFormatters(FormatterRegistry registry) {
403403
}
404404

405405
/**
406-
* Returns {@link Validator} for validating {@code @ModelAttribute}
407-
* and {@code @RequestBody} arguments of annotated controller methods.
408-
* To configure a custom validation, override {@link #getValidator()}.
406+
* Returns a global {@link Validator} instance for example for validating
407+
* {@code @ModelAttribute} and {@code @RequestBody} method arguments.
408+
* Delegates to {@link #getValidator()} first and if that returns {@code null}
409+
* checks the classpath for the presence of a JSR-303 implementations
410+
* before creating a {@code LocalValidatorFactoryBean}.If a JSR-303
411+
* implementation is not available, a no-op {@link Validator} is returned.
409412
*/
410413
@Bean
411-
Validator mvcValidator() {
414+
public Validator mvcValidator() {
412415
Validator validator = getValidator();
413-
if (validator != null) {
414-
return validator;
415-
}
416-
else if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
417-
Class<?> clazz;
418-
try {
419-
String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean";
420-
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
421-
} catch (ClassNotFoundException e) {
422-
throw new BeanInitializationException("Could not find default validator");
423-
} catch (LinkageError e) {
424-
throw new BeanInitializationException("Could not find default validator");
425-
}
426-
return (Validator) BeanUtils.instantiate(clazz);
427-
}
428-
else {
429-
return new Validator() {
430-
public boolean supports(Class<?> clazz) {
431-
return false;
416+
if (validator == null) {
417+
if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
418+
Class<?> clazz;
419+
try {
420+
String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean";
421+
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
422+
} catch (ClassNotFoundException e) {
423+
throw new BeanInitializationException("Could not find default validator");
424+
} catch (LinkageError e) {
425+
throw new BeanInitializationException("Could not find default validator");
432426
}
433-
public void validate(Object target, Errors errors) {
434-
}
435-
};
427+
validator = (Validator) BeanUtils.instantiate(clazz);
428+
}
429+
else {
430+
validator = new Validator() {
431+
public boolean supports(Class<?> clazz) {
432+
return false;
433+
}
434+
public void validate(Object target, Errors errors) {
435+
}
436+
};
437+
}
436438
}
439+
return validator;
437440
}
438441

439442
/**
@@ -463,11 +466,11 @@ public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
463466

464467
/**
465468
* Returns a {@link HandlerExceptionResolverComposite} that contains a list
466-
* of exception resolvers. To customize the list of exception resolvers,
467-
* override {@link #configureHandlerExceptionResolvers(List)}.
469+
* of exception resolvers. To customize the list of exception resolvers,
470+
* consider overriding {@link #configureHandlerExceptionResolvers(List)}.
468471
*/
469472
@Bean
470-
HandlerExceptionResolver handlerExceptionResolver() throws Exception {
473+
public HandlerExceptionResolver handlerExceptionResolver() throws Exception {
471474
List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<HandlerExceptionResolver>();
472475
configureHandlerExceptionResolvers(exceptionResolvers);
473476

0 commit comments

Comments
 (0)