Skip to content

Commit 40ba95f

Browse files
committed
Consistent configurer access in WebMvcConfigurationSupport
Issue: SPR-16017
1 parent cc70fdc commit 40ba95f

File tree

11 files changed

+161
-98
lines changed

11 files changed

+161
-98
lines changed

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* {@code MediaTypeFileExtensionResolver} instances.
4040
*
4141
* @author Rossen Stoyanchev
42+
* @author Juergen Hoeller
4243
* @since 3.2
4344
*/
4445
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
@@ -65,6 +66,7 @@ public ContentNegotiationManager(ContentNegotiationStrategy... strategies) {
6566
* A collection-based alternative to
6667
* {@link #ContentNegotiationManager(ContentNegotiationStrategy...)}.
6768
* @param strategies the strategies to use
69+
* @since 3.2.2
6870
*/
6971
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
7072
Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
@@ -95,7 +97,7 @@ public List<ContentNegotiationStrategy> getStrategies() {
9597
/**
9698
* Find a {@code ContentNegotiationStrategy} of the given type.
9799
* @param strategyType the strategy type
98-
* @return the first matching strategy or {@code null}.
100+
* @return the first matching strategy, or {@code null} if none
99101
* @since 4.3
100102
*/
101103
@SuppressWarnings("unchecked")

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -85,6 +85,7 @@
8585
* extension to a MediaType. You may {@link #setUseJaf suppress} the use of JAF.
8686
*
8787
* @author Rossen Stoyanchev
88+
* @author Brian Clozel
8889
* @since 3.2
8990
*/
9091
public class ContentNegotiationManagerFactoryBean
@@ -255,8 +256,7 @@ public void afterPropertiesSet() {
255256
if (this.favorPathExtension) {
256257
PathExtensionContentNegotiationStrategy strategy;
257258
if (this.servletContext != null && !isUseJafTurnedOff()) {
258-
strategy = new ServletPathExtensionContentNegotiationStrategy(
259-
this.servletContext, this.mediaTypes);
259+
strategy = new ServletPathExtensionContentNegotiationStrategy(this.servletContext, this.mediaTypes);
260260
}
261261
else {
262262
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
@@ -269,8 +269,7 @@ public void afterPropertiesSet() {
269269
}
270270

271271
if (this.favorParameter) {
272-
ParameterContentNegotiationStrategy strategy =
273-
new ParameterContentNegotiationStrategy(this.mediaTypes);
272+
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes);
274273
strategy.setParameterName(this.parameterName);
275274
strategies.add(strategy);
276275
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.servlet.config.annotation;
1718

1819
import java.util.ArrayList;
@@ -22,7 +23,6 @@
2223

2324
import org.springframework.core.task.AsyncTaskExecutor;
2425
import org.springframework.core.task.SimpleAsyncTaskExecutor;
25-
import org.springframework.util.Assert;
2626
import org.springframework.web.context.request.async.CallableProcessingInterceptor;
2727
import org.springframework.web.context.request.async.DeferredResult;
2828
import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
@@ -82,7 +82,6 @@ public AsyncSupportConfigurer setDefaultTimeout(long timeout) {
8282
* @param interceptors the interceptors to register
8383
*/
8484
public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInterceptor... interceptors) {
85-
Assert.notNull(interceptors, "Interceptors are required");
8685
this.callableInterceptors.addAll(Arrays.asList(interceptors));
8786
return this;
8887
}
@@ -93,7 +92,6 @@ public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInt
9392
* @param interceptors the interceptors to register
9493
*/
9594
public AsyncSupportConfigurer registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors) {
96-
Assert.notNull(interceptors, "Interceptors are required");
9795
this.deferredResultInterceptors.addAll(Arrays.asList(interceptors));
9896
return this;
9997
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.servlet.config.annotation;
1718

1819
import java.util.HashMap;
@@ -32,9 +33,6 @@
3233
* Creates a {@code ContentNegotiationManager} and configures it with
3334
* one or more {@link ContentNegotiationStrategy} instances.
3435
*
35-
* <p>As of 5.0 you can set the exact strategies to use via
36-
* {@link #strategies(List)}.
37-
*
3836
* <p>As an alternative you can also rely on the set of defaults described below
3937
* which can be turned on or off or customized through the methods of this
4038
* builder:
@@ -86,12 +84,14 @@
8684
* of JAF.
8785
*
8886
* @author Rossen Stoyanchev
87+
* @author Brian Clozel
88+
* @author Juergen Hoeller
8989
* @since 3.2
90+
* @see ContentNegotiationManagerFactoryBean
9091
*/
9192
public class ContentNegotiationConfigurer {
9293

93-
private final ContentNegotiationManagerFactoryBean factory =
94-
new ContentNegotiationManagerFactoryBean();
94+
private final ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
9595

9696
private final Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();
9797

@@ -226,18 +226,32 @@ public ContentNegotiationConfigurer defaultContentType(MediaType defaultContentT
226226
* Set a custom {@link ContentNegotiationStrategy} to use to determine
227227
* the content type to use when no content type is requested.
228228
* <p>By default this is not set.
229-
* @see #defaultContentType
230229
* @since 4.1.2
230+
* @see #defaultContentType
231231
*/
232232
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
233233
this.factory.setDefaultContentTypeStrategy(defaultStrategy);
234234
return this;
235235
}
236236

237-
protected ContentNegotiationManager getContentNegotiationManager() throws Exception {
237+
238+
/**
239+
* Build a {@link ContentNegotiationManager} based on this configurer's settings.
240+
* @since 4.3.12
241+
* @see ContentNegotiationManagerFactoryBean#getObject()
242+
*/
243+
protected ContentNegotiationManager buildContentNegotiationManager() {
238244
this.factory.addMediaTypes(this.mediaTypes);
239245
this.factory.afterPropertiesSet();
240246
return this.factory.getObject();
241247
}
242248

249+
/**
250+
* @deprecated as of 4.3.12, in favor of {@link #buildContentNegotiationManager()}
251+
*/
252+
@Deprecated
253+
protected ContentNegotiationManager getContentNegotiationManager() throws Exception {
254+
return buildContentNegotiationManager();
255+
}
256+
243257
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
package org.springframework.web.servlet.config.annotation;
1818

19-
import java.util.HashMap;
20-
import java.util.Map;
19+
import java.util.Collections;
2120
import javax.servlet.ServletContext;
2221

2322
import org.springframework.util.Assert;
24-
import org.springframework.web.HttpRequestHandler;
2523
import org.springframework.web.servlet.DispatcherServlet;
2624
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
2725
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
@@ -82,23 +80,30 @@ public void enable(String defaultServletName) {
8280
this.handler.setServletContext(this.servletContext);
8381
}
8482

83+
8584
/**
8685
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
8786
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
8887
* or {@code null} if default servlet handling was not been enabled.
88+
* @since 4.3.12
8989
*/
90-
protected AbstractHandlerMapping getHandlerMapping() {
90+
protected SimpleUrlHandlerMapping buildHandlerMapping() {
9191
if (this.handler == null) {
9292
return null;
9393
}
9494

95-
Map<String, HttpRequestHandler> urlMap = new HashMap<String, HttpRequestHandler>();
96-
urlMap.put("/**", this.handler);
97-
9895
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
96+
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
9997
handlerMapping.setOrder(Integer.MAX_VALUE);
100-
handlerMapping.setUrlMap(urlMap);
10198
return handlerMapping;
10299
}
103100

101+
/**
102+
* @deprecated as of 4.3.12, in favor of {@link #buildHandlerMapping()}
103+
*/
104+
@Deprecated
105+
protected AbstractHandlerMapping getHandlerMapping() {
106+
return buildHandlerMapping();
107+
}
108+
104109
}

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

Lines changed: 3 additions & 4 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-2017 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.
@@ -78,9 +78,7 @@ public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch)
7878
* <p>By default this is set to "false".
7979
* @see WebMvcConfigurer#configureContentNegotiation
8080
*/
81-
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(
82-
Boolean registeredSuffixPatternMatch) {
83-
81+
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) {
8482
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
8583
return this;
8684
}
@@ -106,6 +104,7 @@ public PathMatchConfigurer setPathMatcher(PathMatcher pathMatcher) {
106104
return this;
107105
}
108106

107+
109108
public Boolean isUseSuffixPatternMatch() {
110109
return this.suffixPatternMatch;
111110
}

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

Lines changed: 33 additions & 8 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-2017 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.
@@ -36,14 +36,27 @@
3636
*/
3737
public class ViewControllerRegistry {
3838

39+
private ApplicationContext applicationContext;
40+
3941
private final List<ViewControllerRegistration> registrations = new ArrayList<ViewControllerRegistration>(4);
4042

4143
private final List<RedirectViewControllerRegistration> redirectRegistrations =
4244
new ArrayList<RedirectViewControllerRegistration>(10);
4345

4446
private int order = 1;
4547

46-
private ApplicationContext applicationContext;
48+
49+
/**
50+
* Class constructor with {@link ApplicationContext}.
51+
* @since 4.3.12
52+
*/
53+
public ViewControllerRegistry(ApplicationContext applicationContext) {
54+
this.applicationContext = applicationContext;
55+
}
56+
57+
@Deprecated
58+
public ViewControllerRegistry() {
59+
}
4760

4861

4962
/**
@@ -96,30 +109,42 @@ public void setOrder(int order) {
96109
this.order = order;
97110
}
98111

99-
protected void setApplicationContext(ApplicationContext applicationContext) {
100-
this.applicationContext = applicationContext;
101-
}
102-
103112

104113
/**
105114
* Return the {@code HandlerMapping} that contains the registered view
106115
* controller mappings, or {@code null} for no registrations.
116+
* @since 4.3.12
107117
*/
108-
protected AbstractHandlerMapping getHandlerMapping() {
118+
protected SimpleUrlHandlerMapping buildHandlerMapping() {
109119
if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
110120
return null;
111121
}
122+
112123
Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
113124
for (ViewControllerRegistration registration : this.registrations) {
114125
urlMap.put(registration.getUrlPath(), registration.getViewController());
115126
}
116127
for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
117128
urlMap.put(registration.getUrlPath(), registration.getViewController());
118129
}
130+
119131
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
120-
handlerMapping.setOrder(this.order);
121132
handlerMapping.setUrlMap(urlMap);
133+
handlerMapping.setOrder(this.order);
122134
return handlerMapping;
123135
}
124136

137+
/**
138+
* @deprecated as of 4.3.12, in favor of {@link #buildHandlerMapping()}
139+
*/
140+
@Deprecated
141+
protected AbstractHandlerMapping getHandlerMapping() {
142+
return buildHandlerMapping();
143+
}
144+
145+
@Deprecated
146+
protected void setApplicationContext(ApplicationContext applicationContext) {
147+
this.applicationContext = applicationContext;
148+
}
149+
125150
}

0 commit comments

Comments
 (0)