Skip to content

Commit a1a7c62

Browse files
committed
Consistent configurer access in WebMvcConfigurationSupport
Issue: SPR-16017 (cherry picked from commit 40ba95f)
1 parent 00c0d78 commit a1a7c62

File tree

12 files changed

+104
-97
lines changed

12 files changed

+104
-97
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
@@ -40,6 +40,7 @@
4040
* {@code MediaTypeFileExtensionResolver} instances.
4141
*
4242
* @author Rossen Stoyanchev
43+
* @author Juergen Hoeller
4344
* @since 3.2
4445
*/
4546
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
@@ -66,6 +67,7 @@ public ContentNegotiationManager(ContentNegotiationStrategy... strategies) {
6667
* A collection-based alternative to
6768
* {@link #ContentNegotiationManager(ContentNegotiationStrategy...)}.
6869
* @param strategies the strategies to use
70+
* @since 3.2.2
6971
*/
7072
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
7173
Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
@@ -96,7 +98,7 @@ public List<ContentNegotiationStrategy> getStrategies() {
9698
/**
9799
* Find a {@code ContentNegotiationStrategy} of the given type.
98100
* @param strategyType the strategy type
99-
* @return the first matching strategy or {@code null}.
101+
* @return the first matching strategy, or {@code null} if none
100102
* @since 4.3
101103
*/
102104
@SuppressWarnings("unchecked")

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
* {@link #setStrategies(List)}.
8787
*
8888
* @author Rossen Stoyanchev
89+
* @author Brian Clozel
8990
* @since 3.2
9091
*/
9192
public class ContentNegotiationManagerFactoryBean
@@ -293,6 +294,10 @@ public void afterPropertiesSet() {
293294
build();
294295
}
295296

297+
/**
298+
* Actually build the {@link ContentNegotiationManager}.
299+
* @since 5.0
300+
*/
296301
public ContentNegotiationManager build() {
297302
List<ContentNegotiationStrategy> strategies = new ArrayList<>();
298303

@@ -303,8 +308,7 @@ public ContentNegotiationManager build() {
303308
if (this.favorPathExtension) {
304309
PathExtensionContentNegotiationStrategy strategy;
305310
if (this.servletContext != null && !useRegisteredExtensionsOnly()) {
306-
strategy = new ServletPathExtensionContentNegotiationStrategy(
307-
this.servletContext, this.mediaTypes);
311+
strategy = new ServletPathExtensionContentNegotiationStrategy(this.servletContext, this.mediaTypes);
308312
}
309313
else {
310314
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
@@ -317,14 +321,13 @@ public ContentNegotiationManager build() {
317321
}
318322

319323
if (this.favorParameter) {
320-
ParameterContentNegotiationStrategy strategy =
321-
new ParameterContentNegotiationStrategy(this.mediaTypes);
324+
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes);
322325
strategy.setParameterName(this.parameterName);
323326
if (this.useRegisteredExtensionsOnly != null) {
324327
strategy.setUseRegisteredExtensionsOnly(this.useRegisteredExtensionsOnly);
325328
}
326329
else {
327-
strategy.setUseRegisteredExtensionsOnly(true); // backwards compatibility
330+
strategy.setUseRegisteredExtensionsOnly(true); // backwards compatibility
328331
}
329332
strategies.add(strategy);
330333
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public ContentNegotiationConfigurer ignoreUnknownPathExtensions(boolean ignore)
186186
}
187187

188188
/**
189-
* @deprecated as of 5.0, in favor of {@link #useRegisteredExtensionsOnly(boolean)}, which
190-
* has reverse behavior.
189+
* @deprecated as of 5.0, in favor of {@link #useRegisteredExtensionsOnly(boolean)}
190+
* which has reverse behavior
191191
*/
192192
@Deprecated
193193
public ContentNegotiationConfigurer useJaf(boolean useJaf) {
@@ -262,7 +262,12 @@ public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiatio
262262
}
263263

264264

265-
protected ContentNegotiationManager getContentNegotiationManager() throws Exception {
265+
/**
266+
* Build a {@link ContentNegotiationManager} based on this configurer's settings.
267+
* @since 4.3.12
268+
* @see ContentNegotiationManagerFactoryBean#getObject()
269+
*/
270+
protected ContentNegotiationManager buildContentNegotiationManager() {
266271
this.factory.addMediaTypes(this.mediaTypes);
267272
return this.factory.build();
268273
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
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.lang.Nullable;
2423
import org.springframework.util.Assert;
25-
import org.springframework.web.HttpRequestHandler;
2624
import org.springframework.web.servlet.DispatcherServlet;
27-
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
2825
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
2926
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
3027

@@ -86,23 +83,22 @@ public void enable(@Nullable String defaultServletName) {
8683
this.handler.setServletContext(this.servletContext);
8784
}
8885

86+
8987
/**
9088
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
9189
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
9290
* or {@code null} if default servlet handling was not been enabled.
91+
* @since 4.3.12
9392
*/
9493
@Nullable
95-
protected AbstractHandlerMapping getHandlerMapping() {
94+
protected SimpleUrlHandlerMapping buildHandlerMapping() {
9695
if (this.handler == null) {
9796
return null;
9897
}
9998

100-
Map<String, HttpRequestHandler> urlMap = new HashMap<>();
101-
urlMap.put("/**", this.handler);
102-
10399
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
100+
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
104101
handlerMapping.setOrder(Integer.MAX_VALUE);
105-
handlerMapping.setUrlMap(urlMap);
106102
return handlerMapping;
107103
}
108104

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ public PathMatchConfigurer setUseTrailingSlashMatch(Boolean trailingSlashMatch)
8484
* <p>By default this is set to "false".
8585
* @see WebMvcConfigurer#configureContentNegotiation
8686
*/
87-
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(
88-
Boolean registeredSuffixPatternMatch) {
89-
87+
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) {
9088
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
9189
return this;
9290
}

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

Lines changed: 18 additions & 11 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.
@@ -24,7 +24,6 @@
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.http.HttpStatus;
2626
import org.springframework.lang.Nullable;
27-
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
2827
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
2928

3029
/**
@@ -37,14 +36,23 @@
3736
*/
3837
public class ViewControllerRegistry {
3938

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

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

4446
private int order = 1;
4547

46-
@Nullable
47-
private ApplicationContext applicationContext;
48+
49+
/**
50+
* Class constructor with {@link ApplicationContext}.
51+
* @since 4.3.12
52+
*/
53+
public ViewControllerRegistry(@Nullable ApplicationContext applicationContext) {
54+
this.applicationContext = applicationContext;
55+
}
4856

4957

5058
/**
@@ -97,30 +105,29 @@ public void setOrder(int order) {
97105
this.order = order;
98106
}
99107

100-
protected void setApplicationContext(@Nullable ApplicationContext applicationContext) {
101-
this.applicationContext = applicationContext;
102-
}
103-
104108

105109
/**
106110
* Return the {@code HandlerMapping} that contains the registered view
107111
* controller mappings, or {@code null} for no registrations.
112+
* @since 4.3.12
108113
*/
109114
@Nullable
110-
protected AbstractHandlerMapping getHandlerMapping() {
115+
protected SimpleUrlHandlerMapping buildHandlerMapping() {
111116
if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
112117
return null;
113118
}
114-
Map<String, Object> urlMap = new LinkedHashMap<>();
119+
120+
Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
115121
for (ViewControllerRegistration registration : this.registrations) {
116122
urlMap.put(registration.getUrlPath(), registration.getViewController());
117123
}
118124
for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
119125
urlMap.put(registration.getUrlPath(), registration.getViewController());
120126
}
127+
121128
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
122-
handlerMapping.setOrder(this.order);
123129
handlerMapping.setUrlMap(urlMap);
130+
handlerMapping.setOrder(this.order);
124131
return handlerMapping;
125132
}
126133

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
*/
5555
public class ViewResolverRegistry {
5656

57+
@Nullable
58+
private ContentNegotiationManager contentNegotiationManager;
59+
60+
@Nullable
61+
private ApplicationContext applicationContext;
62+
5763
@Nullable
5864
private ContentNegotiatingViewResolver contentNegotiatingResolver;
5965

@@ -62,20 +68,18 @@ public class ViewResolverRegistry {
6268
@Nullable
6369
private Integer order;
6470

65-
@Nullable
66-
private ContentNegotiationManager contentNegotiationManager;
67-
68-
@Nullable
69-
private ApplicationContext applicationContext;
7071

72+
/**
73+
* Class constructor with {@link ContentNegotiationManager} and {@link ApplicationContext}.
74+
* @since 4.3.12
75+
*/
76+
public ViewResolverRegistry(
77+
ContentNegotiationManager contentNegotiationManager, @Nullable ApplicationContext context) {
7178

72-
protected void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
7379
this.contentNegotiationManager = contentNegotiationManager;
80+
this.applicationContext = context;
7481
}
7582

76-
protected void setApplicationContext(ApplicationContext applicationContext) {
77-
this.applicationContext = applicationContext;
78-
}
7983

8084
/**
8185
* Whether any view resolvers have been registered.
@@ -84,7 +88,6 @@ public boolean hasRegistrations() {
8488
return (this.contentNegotiatingResolver != null || !this.viewResolvers.isEmpty());
8589
}
8690

87-
8891
/**
8992
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other
9093
* configured view resolvers and select among all selected Views based on
@@ -305,6 +308,7 @@ public FreeMarkerRegistration() {
305308
}
306309
}
307310

311+
308312
private static class GroovyMarkupRegistration extends UrlBasedViewResolverRegistration {
309313

310314
public GroovyMarkupRegistration() {
@@ -313,6 +317,7 @@ public GroovyMarkupRegistration() {
313317
}
314318
}
315319

320+
316321
private static class ScriptRegistration extends UrlBasedViewResolverRegistration {
317322

318323
public ScriptRegistration() {

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,7 @@ public ContentNegotiationManager mvcContentNegotiationManager() {
391391
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
392392
configurer.mediaTypes(getDefaultMediaTypes());
393393
configureContentNegotiation(configurer);
394-
try {
395-
this.contentNegotiationManager = configurer.getContentNegotiationManager();
396-
}
397-
catch (Throwable ex) {
398-
throw new BeanInitializationException("Could not create ContentNegotiationManager", ex);
399-
}
394+
this.contentNegotiationManager = configurer.buildContentNegotiationManager();
400395
}
401396
return this.contentNegotiationManager;
402397
}
@@ -436,11 +431,10 @@ protected void configureContentNegotiation(ContentNegotiationConfigurer configur
436431
*/
437432
@Bean
438433
public HandlerMapping viewControllerHandlerMapping() {
439-
ViewControllerRegistry registry = new ViewControllerRegistry();
440-
registry.setApplicationContext(this.applicationContext);
434+
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
441435
addViewControllers(registry);
442436

443-
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
437+
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
444438
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
445439
handlerMapping.setPathMatcher(mvcPathMatcher());
446440
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
@@ -531,9 +525,9 @@ public HandlerMapping defaultServletHandlerMapping() {
531525
Assert.state(this.servletContext != null, "No ServletContext set");
532526
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
533527
configureDefaultServletHandling(configurer);
534-
AbstractHandlerMapping handlerMapping = configurer.getHandlerMapping();
535-
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
536-
return handlerMapping;
528+
529+
HandlerMapping handlerMapping = configurer.buildHandlerMapping();
530+
return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
537531
}
538532

539533
/**
@@ -975,14 +969,11 @@ protected ExceptionHandlerExceptionResolver createExceptionHandlerExceptionResol
975969
*/
976970
@Bean
977971
public ViewResolver mvcViewResolver() {
978-
ViewResolverRegistry registry = new ViewResolverRegistry();
979-
registry.setContentNegotiationManager(mvcContentNegotiationManager());
980-
if (this.applicationContext != null) {
981-
registry.setApplicationContext(this.applicationContext);
982-
}
972+
ViewResolverRegistry registry = new ViewResolverRegistry(
973+
mvcContentNegotiationManager(), this.applicationContext);
983974
configureViewResolvers(registry);
984975

985-
if (registry.getViewResolvers().isEmpty()) {
976+
if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
986977
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
987978
this.applicationContext, ViewResolver.class, true, false);
988979
if (names.length == 1) {
@@ -993,7 +984,9 @@ public ViewResolver mvcViewResolver() {
993984
ViewResolverComposite composite = new ViewResolverComposite();
994985
composite.setOrder(registry.getOrder());
995986
composite.setViewResolvers(registry.getViewResolvers());
996-
composite.setApplicationContext(this.applicationContext);
987+
if (this.applicationContext != null) {
988+
composite.setApplicationContext(this.applicationContext);
989+
}
997990
if (this.servletContext != null) {
998991
composite.setServletContext(this.servletContext);
999992
}

0 commit comments

Comments
 (0)