Skip to content

Commit c778868

Browse files
committed
WebMvcConfigurationSupport uses static NoOpValidator instead of anonymous inner class
1 parent 5cd59d0 commit c778868

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

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

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import javax.xml.transform.Source;
2727

2828
import org.springframework.beans.BeanUtils;
29-
import org.springframework.beans.BeansException;
3029
import org.springframework.beans.factory.BeanInitializationException;
3130
import org.springframework.context.ApplicationContext;
3231
import org.springframework.context.ApplicationContextAware;
@@ -208,17 +207,17 @@ public void setServletContext(ServletContext servletContext) {
208207
* Set the Spring {@link ApplicationContext}, e.g. for resource loading.
209208
*/
210209
@Override
211-
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
210+
public void setApplicationContext(ApplicationContext applicationContext) {
212211
this.applicationContext = applicationContext;
213212
}
214213

214+
215215
/**
216216
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
217217
* requests to annotated controllers.
218218
*/
219219
@Bean
220220
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
221-
222221
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
223222
handlerMapping.setOrder(0);
224223
handlerMapping.setInterceptors(getInterceptors());
@@ -265,7 +264,7 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
265264
* use {@link #addInterceptors(InterceptorRegistry)} instead.
266265
*/
267266
protected final Object[] getInterceptors() {
268-
if (interceptors == null) {
267+
if (this.interceptors == null) {
269268
InterceptorRegistry registry = new InterceptorRegistry();
270269
addInterceptors(registry);
271270
registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
@@ -336,7 +335,7 @@ public HandlerMapping viewControllerHandlerMapping() {
336335
addViewControllers(registry);
337336

338337
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
339-
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
338+
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
340339
handlerMapping.setPathMatcher(mvcPathMatcher());
341340
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
342341
handlerMapping.setInterceptors(getInterceptors());
@@ -369,11 +368,11 @@ public BeanNameUrlHandlerMapping beanNameHandlerMapping() {
369368
*/
370369
@Bean
371370
public HandlerMapping resourceHandlerMapping() {
372-
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(
373-
this.applicationContext, this.servletContext);
371+
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext);
374372
addResourceHandlers(registry);
373+
375374
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
376-
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
375+
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
377376
handlerMapping.setPathMatcher(mvcPathMatcher());
378377
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
379378
return handlerMapping;
@@ -509,24 +508,16 @@ public Validator mvcValidator() {
509508
String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
510509
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
511510
}
512-
catch (ClassNotFoundException e) {
513-
throw new BeanInitializationException("Could not find default validator", e);
511+
catch (ClassNotFoundException ex) {
512+
throw new BeanInitializationException("Could not find default validator class", ex);
514513
}
515-
catch (LinkageError e) {
516-
throw new BeanInitializationException("Could not find default validator", e);
514+
catch (LinkageError ex) {
515+
throw new BeanInitializationException("Could not load default validator class", ex);
517516
}
518517
validator = (Validator) BeanUtils.instantiate(clazz);
519518
}
520519
else {
521-
validator = new Validator() {
522-
@Override
523-
public boolean supports(Class<?> clazz) {
524-
return false;
525-
}
526-
@Override
527-
public void validate(Object target, Errors errors) {
528-
}
529-
};
520+
validator = new NoOpValidator();
530521
}
531522
}
532523
return validator;
@@ -618,14 +609,14 @@ protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> retu
618609
* used to add default message converters.
619610
*/
620611
protected final List<HttpMessageConverter<?>> getMessageConverters() {
621-
if (messageConverters == null) {
622-
messageConverters = new ArrayList<HttpMessageConverter<?>>();
623-
configureMessageConverters(messageConverters);
624-
if (messageConverters.isEmpty()) {
625-
addDefaultHttpMessageConverters(messageConverters);
612+
if (this.messageConverters == null) {
613+
this.messageConverters = new ArrayList<HttpMessageConverter<?>>();
614+
configureMessageConverters(this.messageConverters);
615+
if (this.messageConverters.isEmpty()) {
616+
addDefaultHttpMessageConverters(this.messageConverters);
626617
}
627618
}
628-
return messageConverters;
619+
return this.messageConverters;
629620
}
630621

631622
/**
@@ -791,11 +782,9 @@ protected final void addDefaultHandlerExceptionResolvers(List<HandlerExceptionRe
791782
* resolution is used in which case the order is raised to
792783
* {@link org.springframework.core.Ordered#HIGHEST_PRECEDENCE
793784
* Ordered.HIGHEST_PRECEDENCE}.
794-
*
795785
* <p>If no other resolvers are configured,
796786
* {@link ViewResolverComposite#resolveViewName(String, Locale)} returns null in order
797787
* to allow other potential {@link ViewResolver} beans to resolve views.
798-
*
799788
* @since 4.1
800789
*/
801790
@Bean
@@ -821,12 +810,25 @@ protected void configureViewResolvers(ViewResolverRegistry registry) {
821810
}
822811

823812

824-
private final static class EmptyHandlerMapping extends AbstractHandlerMapping {
813+
private static final class EmptyHandlerMapping extends AbstractHandlerMapping {
825814

826815
@Override
827-
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
816+
protected Object getHandlerInternal(HttpServletRequest request) {
828817
return null;
829818
}
830819
}
831820

821+
822+
private static final class NoOpValidator implements Validator {
823+
824+
@Override
825+
public boolean supports(Class<?> clazz) {
826+
return false;
827+
}
828+
829+
@Override
830+
public void validate(Object target, Errors errors) {
831+
}
832+
}
833+
832834
}

0 commit comments

Comments
 (0)