@@ -403,37 +403,40 @@ protected void addFormatters(FormatterRegistry registry) {
403
403
}
404
404
405
405
/**
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.
409
412
*/
410
413
@ Bean
411
- Validator mvcValidator () {
414
+ public Validator mvcValidator () {
412
415
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" );
432
426
}
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
+ }
436
438
}
439
+ return validator ;
437
440
}
438
441
439
442
/**
@@ -463,11 +466,11 @@ public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
463
466
464
467
/**
465
468
* 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)}.
468
471
*/
469
472
@ Bean
470
- HandlerExceptionResolver handlerExceptionResolver () throws Exception {
473
+ public HandlerExceptionResolver handlerExceptionResolver () throws Exception {
471
474
List <HandlerExceptionResolver > exceptionResolvers = new ArrayList <HandlerExceptionResolver >();
472
475
configureHandlerExceptionResolvers (exceptionResolvers );
473
476
0 commit comments