2222import java .util .HashMap ;
2323import java .util .List ;
2424import java .util .Map ;
25- import javax .servlet .ServletContext ;
2625import javax .servlet .ServletException ;
2726import javax .servlet .ServletResponse ;
2827import javax .servlet .http .HttpServletRequest ;
3231import org .apache .commons .logging .LogFactory ;
3332
3433import org .springframework .beans .factory .InitializingBean ;
34+ import org .springframework .beans .factory .SmartInitializingSingleton ;
3535import org .springframework .core .io .Resource ;
3636import org .springframework .core .io .support .ResourceRegion ;
3737import org .springframework .http .HttpHeaders ;
9191 * @since 3.0.4
9292 */
9393public class ResourceHttpRequestHandler extends WebContentGenerator
94- implements HttpRequestHandler , InitializingBean , CorsConfigurationSource {
94+ implements HttpRequestHandler , InitializingBean , SmartInitializingSingleton , CorsConfigurationSource {
9595
9696 // Servlet 3.1 setContentLengthLong(long) available?
9797 private static final boolean contentLengthLongAvailable =
@@ -112,9 +112,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
112112
113113 private ContentNegotiationManager contentNegotiationManager ;
114114
115- private ServletPathExtensionContentNegotiationStrategy pathExtensionStrategy ;
116-
117- private ServletContext servletContext ;
115+ private PathExtensionContentNegotiationStrategy pathExtensionStrategy ;
118116
119117 private CorsConfiguration corsConfiguration ;
120118
@@ -248,11 +246,6 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
248246 return this .corsConfiguration ;
249247 }
250248
251- @ Override
252- protected void initServletContext (ServletContext servletContext ) {
253- this .servletContext = servletContext ;
254- }
255-
256249
257250 @ Override
258251 public void afterPropertiesSet () throws Exception {
@@ -270,7 +263,6 @@ public void afterPropertiesSet() throws Exception {
270263 if (this .resourceRegionHttpMessageConverter == null ) {
271264 this .resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter ();
272265 }
273- this .pathExtensionStrategy = initPathExtensionStrategy ();
274266 }
275267
276268 /**
@@ -293,7 +285,12 @@ protected void initAllowedLocations() {
293285 }
294286 }
295287
296- protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrategy () {
288+ @ Override
289+ public void afterSingletonsInstantiated () {
290+ this .pathExtensionStrategy = initContentNegotiationStrategy ();
291+ }
292+
293+ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy () {
297294 Map <String , MediaType > mediaTypes = null ;
298295 if (getContentNegotiationManager () != null ) {
299296 PathExtensionContentNegotiationStrategy strategy =
@@ -302,7 +299,9 @@ protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrate
302299 mediaTypes = new HashMap <>(strategy .getMediaTypes ());
303300 }
304301 }
305- return new ServletPathExtensionContentNegotiationStrategy (this .servletContext , mediaTypes );
302+ return (getServletContext () != null ) ?
303+ new ServletPathExtensionContentNegotiationStrategy (getServletContext (), mediaTypes ) :
304+ new PathExtensionContentNegotiationStrategy (mediaTypes );
306305 }
307306
308307
0 commit comments