22
22
import java .util .HashMap ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
- import javax .servlet .ServletContext ;
26
25
import javax .servlet .ServletException ;
27
26
import javax .servlet .ServletResponse ;
28
27
import javax .servlet .http .HttpServletRequest ;
32
31
import org .apache .commons .logging .LogFactory ;
33
32
34
33
import org .springframework .beans .factory .InitializingBean ;
34
+ import org .springframework .beans .factory .SmartInitializingSingleton ;
35
35
import org .springframework .core .io .Resource ;
36
36
import org .springframework .core .io .support .ResourceRegion ;
37
37
import org .springframework .http .HttpHeaders ;
91
91
* @since 3.0.4
92
92
*/
93
93
public class ResourceHttpRequestHandler extends WebContentGenerator
94
- implements HttpRequestHandler , InitializingBean , CorsConfigurationSource {
94
+ implements HttpRequestHandler , InitializingBean , SmartInitializingSingleton , CorsConfigurationSource {
95
95
96
96
// Servlet 3.1 setContentLengthLong(long) available?
97
97
private static final boolean contentLengthLongAvailable =
@@ -112,9 +112,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
112
112
113
113
private ContentNegotiationManager contentNegotiationManager ;
114
114
115
- private ServletPathExtensionContentNegotiationStrategy pathExtensionStrategy ;
116
-
117
- private ServletContext servletContext ;
115
+ private PathExtensionContentNegotiationStrategy pathExtensionStrategy ;
118
116
119
117
private CorsConfiguration corsConfiguration ;
120
118
@@ -248,11 +246,6 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
248
246
return this .corsConfiguration ;
249
247
}
250
248
251
- @ Override
252
- protected void initServletContext (ServletContext servletContext ) {
253
- this .servletContext = servletContext ;
254
- }
255
-
256
249
257
250
@ Override
258
251
public void afterPropertiesSet () throws Exception {
@@ -270,7 +263,6 @@ public void afterPropertiesSet() throws Exception {
270
263
if (this .resourceRegionHttpMessageConverter == null ) {
271
264
this .resourceRegionHttpMessageConverter = new ResourceRegionHttpMessageConverter ();
272
265
}
273
- this .pathExtensionStrategy = initPathExtensionStrategy ();
274
266
}
275
267
276
268
/**
@@ -293,7 +285,12 @@ protected void initAllowedLocations() {
293
285
}
294
286
}
295
287
296
- protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrategy () {
288
+ @ Override
289
+ public void afterSingletonsInstantiated () {
290
+ this .pathExtensionStrategy = initContentNegotiationStrategy ();
291
+ }
292
+
293
+ protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy () {
297
294
Map <String , MediaType > mediaTypes = null ;
298
295
if (getContentNegotiationManager () != null ) {
299
296
PathExtensionContentNegotiationStrategy strategy =
@@ -302,7 +299,9 @@ protected ServletPathExtensionContentNegotiationStrategy initPathExtensionStrate
302
299
mediaTypes = new HashMap <>(strategy .getMediaTypes ());
303
300
}
304
301
}
305
- return new ServletPathExtensionContentNegotiationStrategy (this .servletContext , mediaTypes );
302
+ return (getServletContext () != null ) ?
303
+ new ServletPathExtensionContentNegotiationStrategy (getServletContext (), mediaTypes ) :
304
+ new PathExtensionContentNegotiationStrategy (mediaTypes );
306
305
}
307
306
308
307
0 commit comments