Skip to content

Commit b7d3a96

Browse files
committed
ResourceHttpRequestHandler initializes PathExtensionContentNegotiationStrategy in afterPropertiesSet
Issue: SPR-14851
1 parent dbaafdd commit b7d3a96

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ protected AbstractHandlerMapping getHandlerMapping() {
145145
handler.setContentNegotiationManager(this.contentNegotiationManager);
146146
try {
147147
handler.afterPropertiesSet();
148-
handler.afterSingletonsInstantiated();
149148
}
150-
catch (Exception ex) {
149+
catch (Throwable ex) {
151150
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
152151
}
153152
urlMap.put(pathPattern, handler);

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.apache.commons.logging.LogFactory;
3232

3333
import org.springframework.beans.factory.InitializingBean;
34-
import org.springframework.beans.factory.SmartInitializingSingleton;
3534
import org.springframework.core.io.Resource;
3635
import org.springframework.core.io.support.ResourceRegion;
3736
import org.springframework.http.HttpHeaders;
@@ -91,7 +90,7 @@
9190
* @since 3.0.4
9291
*/
9392
public class ResourceHttpRequestHandler extends WebContentGenerator
94-
implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource {
93+
implements HttpRequestHandler, InitializingBean, CorsConfigurationSource {
9594

9695
// Servlet 3.1 setContentLengthLong(long) available?
9796
private static final boolean contentLengthLongAvailable =
@@ -112,7 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
112111

113112
private ContentNegotiationManager contentNegotiationManager;
114113

115-
private PathExtensionContentNegotiationStrategy pathExtensionStrategy;
114+
private PathExtensionContentNegotiationStrategy contentNegotiationStrategy;
116115

117116
private CorsConfiguration corsConfiguration;
118117

@@ -253,16 +252,20 @@ public void afterPropertiesSet() throws Exception {
253252
logger.warn("Locations list is empty. No resources will be served unless a " +
254253
"custom ResourceResolver is configured as an alternative to PathResourceResolver.");
255254
}
255+
256256
if (this.resourceResolvers.isEmpty()) {
257257
this.resourceResolvers.add(new PathResourceResolver());
258258
}
259259
initAllowedLocations();
260+
260261
if (this.resourceHttpMessageConverter == null) {
261262
this.resourceHttpMessageConverter = new ResourceHttpMessageConverter();
262263
}
263264
if (this.resourceRegionHttpMessageConverter == null) {
264265
this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter();
265266
}
267+
268+
this.contentNegotiationStrategy = initContentNegotiationStrategy();
266269
}
267270

268271
/**
@@ -285,11 +288,12 @@ protected void initAllowedLocations() {
285288
}
286289
}
287290

288-
@Override
289-
public void afterSingletonsInstantiated() {
290-
this.pathExtensionStrategy = initContentNegotiationStrategy();
291-
}
292-
291+
/**
292+
* Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager}
293+
* setup and the availability of a {@code ServletContext}.
294+
* @see ServletPathExtensionContentNegotiationStrategy
295+
* @see PathExtensionContentNegotiationStrategy
296+
*/
293297
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() {
294298
Map<String, MediaType> mediaTypes = null;
295299
if (getContentNegotiationManager() != null) {
@@ -299,9 +303,9 @@ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy
299303
mediaTypes = new HashMap<>(strategy.getMediaTypes());
300304
}
301305
}
302-
return (getServletContext() != null) ?
303-
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
304-
new PathExtensionContentNegotiationStrategy(mediaTypes);
306+
return (getServletContext() != null ?
307+
new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
308+
new PathExtensionContentNegotiationStrategy(mediaTypes));
305309
}
306310

307311

@@ -514,7 +518,7 @@ protected boolean isInvalidPath(String path) {
514518
* @return the corresponding media type, or {@code null} if none found
515519
*/
516520
protected MediaType getMediaType(HttpServletRequest request, Resource resource) {
517-
return this.pathExtensionStrategy.getMediaTypeForResource(resource);
521+
return this.contentNegotiationStrategy.getMediaTypeForResource(resource);
518522
}
519523

520524
/**

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public void setUp() throws Exception {
8181
this.handler.setCacheSeconds(3600);
8282
this.handler.setServletContext(new TestServletContext());
8383
this.handler.afterPropertiesSet();
84-
this.handler.afterSingletonsInstantiated();
8584

8685
this.request = new MockHttpServletRequest("GET", "");
8786
this.response = new MockHttpServletResponse();
@@ -148,7 +147,6 @@ public void getVersionedResource() throws Exception {
148147
.addFixedVersionStrategy("versionString", "/**");
149148
this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver()));
150149
this.handler.afterPropertiesSet();
151-
this.handler.afterSingletonsInstantiated();
152150

153151
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css");
154152
this.handler.handleRequest(this.request, this.response);
@@ -255,7 +253,6 @@ public void getResourceWithRegisteredMediaType() throws Exception {
255253
handler.setLocations(paths);
256254
handler.setContentNegotiationManager(manager);
257255
handler.afterPropertiesSet();
258-
handler.afterSingletonsInstantiated();
259256

260257
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
261258
handler.handleRequest(this.request, this.response);
@@ -277,7 +274,6 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
277274
handler.setLocations(paths);
278275
handler.setContentNegotiationManager(manager);
279276
handler.afterPropertiesSet();
280-
handler.afterSingletonsInstantiated();
281277

282278
this.request.addHeader("Accept", "application/json,text/plain,*/*");
283279
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
@@ -306,7 +302,6 @@ public String getVirtualServerName() {
306302
handler.setServletContext(servletContext);
307303
handler.setLocations(paths);
308304
handler.afterPropertiesSet();
309-
handler.afterSingletonsInstantiated();
310305

311306
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
312307
handler.handleRequest(this.request, this.response);
@@ -421,7 +416,6 @@ public void initAllowedLocationsWithExplicitConfiguration() throws Exception {
421416
handler.setServletContext(new MockServletContext());
422417
handler.setLocations(Arrays.asList(location1, location2));
423418
handler.afterPropertiesSet();
424-
handler.afterSingletonsInstantiated();
425419

426420
Resource[] locations = pathResolver.getAllowedLocations();
427421
assertEquals(1, locations.length);

0 commit comments

Comments
 (0)