diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java index 70c4f961841..7cd4e2a32f0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java @@ -145,9 +145,8 @@ public class ResourceHandlerRegistry { handler.setContentNegotiationManager(this.contentNegotiationManager); try { handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); } - catch (Exception ex) { + catch (Throwable ex) { throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); } urlMap.put(pathPattern, handler); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index fc3c1fa59a2..84a6523a461 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -31,7 +31,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourceRegion; import org.springframework.http.HttpHeaders; @@ -91,7 +90,7 @@ import org.springframework.web.servlet.support.WebContentGenerator; * @since 3.0.4 */ public class ResourceHttpRequestHandler extends WebContentGenerator - implements HttpRequestHandler, InitializingBean, SmartInitializingSingleton, CorsConfigurationSource { + implements HttpRequestHandler, InitializingBean, CorsConfigurationSource { // Servlet 3.1 setContentLengthLong(long) available? private static final boolean contentLengthLongAvailable = @@ -112,7 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator private ContentNegotiationManager contentNegotiationManager; - private PathExtensionContentNegotiationStrategy pathExtensionStrategy; + private PathExtensionContentNegotiationStrategy contentNegotiationStrategy; private CorsConfiguration corsConfiguration; @@ -253,16 +252,20 @@ public class ResourceHttpRequestHandler extends WebContentGenerator logger.warn("Locations list is empty. No resources will be served unless a " + "custom ResourceResolver is configured as an alternative to PathResourceResolver."); } + if (this.resourceResolvers.isEmpty()) { this.resourceResolvers.add(new PathResourceResolver()); } initAllowedLocations(); + if (this.resourceHttpMessageConverter == null) { this.resourceHttpMessageConverter = new ResourceHttpMessageConverter(); } if (this.resourceRegionHttpMessageConverter == null) { this.resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter(); } + + this.contentNegotiationStrategy = initContentNegotiationStrategy(); } /** @@ -285,11 +288,12 @@ public class ResourceHttpRequestHandler extends WebContentGenerator } } - @Override - public void afterSingletonsInstantiated() { - this.pathExtensionStrategy = initContentNegotiationStrategy(); - } - + /** + * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager} + * setup and the availability of a {@code ServletContext}. + * @see ServletPathExtensionContentNegotiationStrategy + * @see PathExtensionContentNegotiationStrategy + */ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() { Map mediaTypes = null; if (getContentNegotiationManager() != null) { @@ -299,9 +303,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator mediaTypes = new HashMap<>(strategy.getMediaTypes()); } } - return (getServletContext() != null) ? - new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) : - new PathExtensionContentNegotiationStrategy(mediaTypes); + return (getServletContext() != null ? + new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) : + new PathExtensionContentNegotiationStrategy(mediaTypes)); } @@ -514,7 +518,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator * @return the corresponding media type, or {@code null} if none found */ protected MediaType getMediaType(HttpServletRequest request, Resource resource) { - return this.pathExtensionStrategy.getMediaTypeForResource(resource); + return this.contentNegotiationStrategy.getMediaTypeForResource(resource); } /** diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index 1a5b74f55b7..e835aff8c15 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -81,7 +81,6 @@ public class ResourceHttpRequestHandlerTests { this.handler.setCacheSeconds(3600); this.handler.setServletContext(new TestServletContext()); this.handler.afterPropertiesSet(); - this.handler.afterSingletonsInstantiated(); this.request = new MockHttpServletRequest("GET", ""); this.response = new MockHttpServletResponse(); @@ -148,7 +147,6 @@ public class ResourceHttpRequestHandlerTests { .addFixedVersionStrategy("versionString", "/**"); this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver())); this.handler.afterPropertiesSet(); - this.handler.afterSingletonsInstantiated(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css"); this.handler.handleRequest(this.request, this.response); @@ -255,7 +253,6 @@ public class ResourceHttpRequestHandlerTests { handler.setLocations(paths); handler.setContentNegotiationManager(manager); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); handler.handleRequest(this.request, this.response); @@ -277,7 +274,6 @@ public class ResourceHttpRequestHandlerTests { handler.setLocations(paths); handler.setContentNegotiationManager(manager); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); this.request.addHeader("Accept", "application/json,text/plain,*/*"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); @@ -306,7 +302,6 @@ public class ResourceHttpRequestHandlerTests { handler.setServletContext(servletContext); handler.setLocations(paths); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); handler.handleRequest(this.request, this.response); @@ -421,7 +416,6 @@ public class ResourceHttpRequestHandlerTests { handler.setServletContext(new MockServletContext()); handler.setLocations(Arrays.asList(location1, location2)); handler.afterPropertiesSet(); - handler.afterSingletonsInstantiated(); Resource[] locations = pathResolver.getAllowedLocations(); assertEquals(1, locations.length);