Skip to content

Commit 96f1a0e

Browse files
committed
Polishing
1 parent 78049d3 commit 96f1a0e

File tree

7 files changed

+135
-131
lines changed

7 files changed

+135
-131
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ else if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassL
433433
catch (Throwable ex) {
434434
throw new BeanInitializationException("Could not find default validator class", ex);
435435
}
436-
validator = (Validator) BeanUtils.instantiate(clazz);
436+
validator = (Validator) BeanUtils.instantiateClass(clazz);
437437
}
438438
else {
439439
validator = new Validator() {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,14 +22,15 @@
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.format.FormatterRegistry;
2424
import org.springframework.http.converter.HttpMessageConverter;
25+
import org.springframework.util.CollectionUtils;
2526
import org.springframework.validation.MessageCodesResolver;
2627
import org.springframework.validation.Validator;
2728
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
2829
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
2930
import org.springframework.web.servlet.HandlerExceptionResolver;
3031

3132
/**
32-
* A sub-class of {@code WebMvcConfigurationSupport} that detects and delegates
33+
* A subclass of {@code WebMvcConfigurationSupport} that detects and delegates
3334
* to all beans of type {@link WebMvcConfigurer} allowing them to customize the
3435
* configuration provided by {@code WebMvcConfigurationSupport}. This is the
3536
* class actually imported by {@link EnableWebMvc @EnableWebMvc}.
@@ -45,10 +46,9 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
4546

4647
@Autowired(required = false)
4748
public void setConfigurers(List<WebMvcConfigurer> configurers) {
48-
if (configurers == null || configurers.isEmpty()) {
49-
return;
49+
if (!CollectionUtils.isEmpty(configurers)) {
50+
this.configurers.addWebMvcConfigurers(configurers);
5051
}
51-
this.configurers.addWebMvcConfigurers(configurers);
5252
}
5353

5454

@@ -118,23 +118,23 @@ protected void addFormatters(FormatterRegistry registry) {
118118
}
119119

120120
@Override
121-
protected Validator getValidator() {
122-
return this.configurers.getValidator();
121+
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
122+
this.configurers.configureHandlerExceptionResolvers(exceptionResolvers);
123123
}
124124

125125
@Override
126-
protected MessageCodesResolver getMessageCodesResolver() {
127-
return this.configurers.getMessageCodesResolver();
126+
protected void addCorsMappings(CorsRegistry registry) {
127+
this.configurers.addCorsMappings(registry);
128128
}
129129

130130
@Override
131-
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
132-
this.configurers.configureHandlerExceptionResolvers(exceptionResolvers);
131+
protected Validator getValidator() {
132+
return this.configurers.getValidator();
133133
}
134134

135135
@Override
136-
protected void addCorsMappings(CorsRegistry registry) {
137-
this.configurers.addCorsMappings(registry);
136+
protected MessageCodesResolver getMessageCodesResolver() {
137+
return this.configurers.getMessageCodesResolver();
138138
}
139139

140140
}

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

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.config.annotation;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Locale;
@@ -229,6 +230,7 @@ public ServletContext getServletContext() {
229230
return this.servletContext;
230231
}
231232

233+
232234
/**
233235
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
234236
* requests to annotated controllers.
@@ -251,18 +253,20 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping() {
251253
if (configurer.isUseTrailingSlashMatch() != null) {
252254
handlerMapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch());
253255
}
254-
if (configurer.getPathMatcher() != null) {
255-
handlerMapping.setPathMatcher(configurer.getPathMatcher());
256+
UrlPathHelper pathHelper = configurer.getUrlPathHelper();
257+
if (pathHelper != null) {
258+
handlerMapping.setUrlPathHelper(pathHelper);
256259
}
257-
if (configurer.getUrlPathHelper() != null) {
258-
handlerMapping.setUrlPathHelper(configurer.getUrlPathHelper());
260+
PathMatcher pathMatcher = configurer.getPathMatcher();
261+
if (pathMatcher != null) {
262+
handlerMapping.setPathMatcher(pathMatcher);
259263
}
260264

261265
return handlerMapping;
262266
}
263267

264268
/**
265-
* Protected method for plugging in a custom sub-class of
269+
* Protected method for plugging in a custom subclass of
266270
* {@link RequestMappingHandlerMapping}.
267271
*/
268272
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
@@ -335,7 +339,7 @@ public ContentNegotiationManager mvcContentNegotiationManager() {
335339
}
336340

337341
protected Map<String, MediaType> getDefaultMediaTypes() {
338-
Map<String, MediaType> map = new HashMap<String, MediaType>();
342+
Map<String, MediaType> map = new HashMap<String, MediaType>(4);
339343
if (romePresent) {
340344
map.put("atom", MediaType.APPLICATION_ATOM_XML);
341345
map.put("rss", MediaType.valueOf("application/rss+xml"));
@@ -488,18 +492,14 @@ public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
488492
adapter.setCustomReturnValueHandlers(returnValueHandlers);
489493

490494
if (jackson2Present) {
491-
List<RequestBodyAdvice> requestBodyAdvices = new ArrayList<RequestBodyAdvice>();
492-
requestBodyAdvices.add(new JsonViewRequestBodyAdvice());
493-
adapter.setRequestBodyAdvice(requestBodyAdvices);
494-
495-
List<ResponseBodyAdvice<?>> responseBodyAdvices = new ArrayList<ResponseBodyAdvice<?>>();
496-
responseBodyAdvices.add(new JsonViewResponseBodyAdvice());
497-
adapter.setResponseBodyAdvice(responseBodyAdvices);
495+
adapter.setRequestBodyAdvice(
496+
Collections.<RequestBodyAdvice>singletonList(new JsonViewRequestBodyAdvice()));
497+
adapter.setResponseBodyAdvice(
498+
Collections.<ResponseBodyAdvice<?>>singletonList(new JsonViewResponseBodyAdvice()));
498499
}
499500

500501
AsyncSupportConfigurer configurer = new AsyncSupportConfigurer();
501502
configureAsyncSupport(configurer);
502-
503503
if (configurer.getTaskExecutor() != null) {
504504
adapter.setTaskExecutor(configurer.getTaskExecutor());
505505
}
@@ -524,6 +524,20 @@ protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer
524524
return initializer;
525525
}
526526

527+
/**
528+
* Override this method to provide a custom {@link MessageCodesResolver}.
529+
*/
530+
protected MessageCodesResolver getMessageCodesResolver() {
531+
return null;
532+
}
533+
534+
/**
535+
* Override this method to configure asynchronous request processing options.
536+
* @see AsyncSupportConfigurer
537+
*/
538+
protected void configureAsyncSupport(AsyncSupportConfigurer configurer) {
539+
}
540+
527541
/**
528542
* Return a {@link FormattingConversionService} for use with annotated
529543
* controller methods and the {@code spring:eval} JSP tag.
@@ -536,6 +550,12 @@ public FormattingConversionService mvcConversionService() {
536550
return conversionService;
537551
}
538552

553+
/**
554+
* Override this method to add custom {@link Converter}s and {@link Formatter}s.
555+
*/
556+
protected void addFormatters(FormatterRegistry registry) {
557+
}
558+
539559
/**
540560
* Return a global {@link Validator} instance for example for validating
541561
* {@code @ModelAttribute} and {@code @RequestBody} method arguments.
@@ -560,7 +580,7 @@ public Validator mvcValidator() {
560580
catch (LinkageError ex) {
561581
throw new BeanInitializationException("Could not load default validator class", ex);
562582
}
563-
validator = (Validator) BeanUtils.instantiate(clazz);
583+
validator = (Validator) BeanUtils.instantiateClass(clazz);
564584
}
565585
else {
566586
validator = new NoOpValidator();
@@ -569,6 +589,13 @@ public Validator mvcValidator() {
569589
return validator;
570590
}
571591

592+
/**
593+
* Override this method to provide a custom {@link Validator}.
594+
*/
595+
protected Validator getValidator() {
596+
return null;
597+
}
598+
572599
/**
573600
* Return a global {@link PathMatcher} instance for path matching
574601
* patterns in {@link HandlerMapping}s.
@@ -595,26 +622,8 @@ public PathMatcher mvcPathMatcher() {
595622
*/
596623
@Bean
597624
public UrlPathHelper mvcUrlPathHelper() {
598-
if (getPathMatchConfigurer().getUrlPathHelper() != null) {
599-
return getPathMatchConfigurer().getUrlPathHelper();
600-
}
601-
else {
602-
return new UrlPathHelper();
603-
}
604-
}
605-
606-
/**
607-
* Override this method to provide a custom {@link Validator}.
608-
*/
609-
protected Validator getValidator() {
610-
return null;
611-
}
612-
613-
/**
614-
* Override this method to provide a custom {@link MessageCodesResolver}.
615-
*/
616-
protected MessageCodesResolver getMessageCodesResolver() {
617-
return null;
625+
UrlPathHelper pathHelper = getPathMatchConfigurer().getUrlPathHelper();
626+
return (pathHelper != null ? pathHelper : new UrlPathHelper());
618627
}
619628

620629
/**
@@ -679,6 +688,14 @@ protected final List<HttpMessageConverter<?>> getMessageConverters() {
679688
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
680689
}
681690

691+
/**
692+
* Override this method to extend or modify the list of converters after it
693+
* has been configured. This may be useful for example to allow default
694+
* converters to be registered and then insert a custom converter through
695+
* this method.
696+
* @param converters the list of configured converters to extend.
697+
* @since 4.1.3
698+
*/
682699
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
683700
}
684701

@@ -719,19 +736,6 @@ else if (gsonPresent) {
719736
}
720737
}
721738

722-
/**
723-
* Override this method to add custom {@link Converter}s and {@link Formatter}s.
724-
*/
725-
protected void addFormatters(FormatterRegistry registry) {
726-
}
727-
728-
/**
729-
* Override this method to configure asynchronous request processing options.
730-
* @see AsyncSupportConfigurer
731-
*/
732-
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
733-
}
734-
735739
/**
736740
* Return an instance of {@link CompositeUriComponentsContributor} for use with
737741
* {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
@@ -774,11 +778,9 @@ public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
774778
public HandlerExceptionResolver handlerExceptionResolver() {
775779
List<HandlerExceptionResolver> exceptionResolvers = new ArrayList<HandlerExceptionResolver>();
776780
configureHandlerExceptionResolvers(exceptionResolvers);
777-
778781
if (exceptionResolvers.isEmpty()) {
779782
addDefaultHandlerExceptionResolvers(exceptionResolvers);
780783
}
781-
782784
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
783785
composite.setOrder(0);
784786
composite.setExceptionResolvers(exceptionResolvers);

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,14 +73,6 @@ public interface WebMvcConfigurer {
7373
*/
7474
void extendMessageConverters(List<HttpMessageConverter<?>> converters);
7575

76-
/**
77-
* Provide a custom {@link Validator} instead of the one created by default.
78-
* The default implementation, assuming JSR-303 is on the classpath, is:
79-
* {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
80-
* Leave the return value as {@code null} to keep the default.
81-
*/
82-
Validator getValidator();
83-
8476
/**
8577
* Configure content negotiation options.
8678
*/
@@ -144,13 +136,6 @@ public interface WebMvcConfigurer {
144136
*/
145137
void addInterceptors(InterceptorRegistry registry);
146138

147-
/**
148-
* Provide a custom {@link MessageCodesResolver} for building message codes
149-
* from data binding and validation error codes. Leave the return value as
150-
* {@code null} to keep the default.
151-
*/
152-
MessageCodesResolver getMessageCodesResolver();
153-
154139
/**
155140
* Configure simple automated controllers pre-configured with the response
156141
* status code and/or a view to render the response body. This is useful in
@@ -188,4 +173,19 @@ public interface WebMvcConfigurer {
188173
*/
189174
void addCorsMappings(CorsRegistry registry);
190175

176+
/**
177+
* Provide a custom {@link Validator} instead of the one created by default.
178+
* The default implementation, assuming JSR-303 is on the classpath, is:
179+
* {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}.
180+
* Leave the return value as {@code null} to keep the default.
181+
*/
182+
Validator getValidator();
183+
184+
/**
185+
* Provide a custom {@link MessageCodesResolver} for building message codes
186+
* from data binding and validation error codes. Leave the return value as
187+
* {@code null} to keep the default.
188+
*/
189+
MessageCodesResolver getMessageCodesResolver();
190+
191191
}

0 commit comments

Comments
 (0)