31
31
import org .apache .commons .logging .LogFactory ;
32
32
33
33
import org .springframework .beans .factory .InitializingBean ;
34
- import org .springframework .beans .factory .SmartInitializingSingleton ;
35
34
import org .springframework .core .io .Resource ;
36
35
import org .springframework .core .io .support .ResourceRegion ;
37
36
import org .springframework .http .HttpHeaders ;
91
90
* @since 3.0.4
92
91
*/
93
92
public class ResourceHttpRequestHandler extends WebContentGenerator
94
- implements HttpRequestHandler , InitializingBean , SmartInitializingSingleton , CorsConfigurationSource {
93
+ implements HttpRequestHandler , InitializingBean , CorsConfigurationSource {
95
94
96
95
// Servlet 3.1 setContentLengthLong(long) available?
97
96
private static final boolean contentLengthLongAvailable =
@@ -112,7 +111,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
112
111
113
112
private ContentNegotiationManager contentNegotiationManager ;
114
113
115
- private PathExtensionContentNegotiationStrategy pathExtensionStrategy ;
114
+ private PathExtensionContentNegotiationStrategy contentNegotiationStrategy ;
116
115
117
116
private CorsConfiguration corsConfiguration ;
118
117
@@ -253,16 +252,20 @@ public void afterPropertiesSet() throws Exception {
253
252
logger .warn ("Locations list is empty. No resources will be served unless a " +
254
253
"custom ResourceResolver is configured as an alternative to PathResourceResolver." );
255
254
}
255
+
256
256
if (this .resourceResolvers .isEmpty ()) {
257
257
this .resourceResolvers .add (new PathResourceResolver ());
258
258
}
259
259
initAllowedLocations ();
260
+
260
261
if (this .resourceHttpMessageConverter == null ) {
261
262
this .resourceHttpMessageConverter = new ResourceHttpMessageConverter ();
262
263
}
263
264
if (this .resourceRegionHttpMessageConverter == null ) {
264
265
this .resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter ();
265
266
}
267
+
268
+ this .contentNegotiationStrategy = initContentNegotiationStrategy ();
266
269
}
267
270
268
271
/**
@@ -285,11 +288,12 @@ protected void initAllowedLocations() {
285
288
}
286
289
}
287
290
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
+ */
293
297
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy () {
294
298
Map <String , MediaType > mediaTypes = null ;
295
299
if (getContentNegotiationManager () != null ) {
@@ -299,9 +303,9 @@ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy
299
303
mediaTypes = new HashMap <>(strategy .getMediaTypes ());
300
304
}
301
305
}
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 ) );
305
309
}
306
310
307
311
@@ -514,7 +518,7 @@ protected boolean isInvalidPath(String path) {
514
518
* @return the corresponding media type, or {@code null} if none found
515
519
*/
516
520
protected MediaType getMediaType (HttpServletRequest request , Resource resource ) {
517
- return this .pathExtensionStrategy .getMediaTypeForResource (resource );
521
+ return this .contentNegotiationStrategy .getMediaTypeForResource (resource );
518
522
}
519
523
520
524
/**
0 commit comments