84
84
import org .springframework .web .accept .ContentNegotiationManager ;
85
85
import org .springframework .web .accept .ContentNegotiationStrategy ;
86
86
import org .springframework .web .bind .support .ConfigurableWebBindingInitializer ;
87
+ import org .springframework .web .context .ServletContextAware ;
87
88
import org .springframework .web .context .request .NativeWebRequest ;
88
89
import org .springframework .web .context .request .RequestAttributes ;
89
90
import org .springframework .web .context .request .RequestContextListener ;
@@ -182,7 +183,11 @@ public OrderedFormContentFilter formContentFilter() {
182
183
@ EnableConfigurationProperties ({ WebMvcProperties .class ,
183
184
org .springframework .boot .autoconfigure .web .ResourceProperties .class , WebProperties .class })
184
185
@ Order (0 )
185
- public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
186
+ public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer , ServletContextAware {
187
+
188
+ private static final Log logger = LogFactory .getLog (WebMvcConfigurer .class );
189
+
190
+ private final Resources resourceProperties ;
186
191
187
192
private final WebMvcProperties mvcProperties ;
188
193
@@ -194,13 +199,19 @@ public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
194
199
195
200
private final ObjectProvider <ServletRegistrationBean <?>> servletRegistrations ;
196
201
197
- final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer ;
202
+ private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer ;
203
+
204
+ private ServletContext servletContext ;
198
205
199
- public WebMvcAutoConfigurationAdapter (WebProperties webProperties , WebMvcProperties mvcProperties ,
200
- ListableBeanFactory beanFactory , ObjectProvider <HttpMessageConverters > messageConvertersProvider ,
206
+ public WebMvcAutoConfigurationAdapter (
207
+ org .springframework .boot .autoconfigure .web .ResourceProperties resourceProperties ,
208
+ WebProperties webProperties , WebMvcProperties mvcProperties , ListableBeanFactory beanFactory ,
209
+ ObjectProvider <HttpMessageConverters > messageConvertersProvider ,
201
210
ObjectProvider <ResourceHandlerRegistrationCustomizer > resourceHandlerRegistrationCustomizerProvider ,
202
211
ObjectProvider <DispatcherServletPath > dispatcherServletPath ,
203
212
ObjectProvider <ServletRegistrationBean <?>> servletRegistrations ) {
213
+ this .resourceProperties = resourceProperties .hasBeenCustomized () ? resourceProperties
214
+ : webProperties .getResources ();
204
215
this .mvcProperties = mvcProperties ;
205
216
this .beanFactory = beanFactory ;
206
217
this .messageConvertersProvider = messageConvertersProvider ;
@@ -210,6 +221,11 @@ public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcPropert
210
221
this .mvcProperties .checkConfiguration ();
211
222
}
212
223
224
+ @ Override
225
+ public void setServletContext (ServletContext servletContext ) {
226
+ this .servletContext = servletContext ;
227
+ }
228
+
213
229
@ Override
214
230
public void configureMessageConverters (List <HttpMessageConverter <?>> converters ) {
215
231
this .messageConvertersProvider
@@ -312,6 +328,49 @@ public void addFormatters(FormatterRegistry registry) {
312
328
ApplicationConversionService .addBeans (registry , this .beanFactory );
313
329
}
314
330
331
+ @ Override
332
+ public void addResourceHandlers (ResourceHandlerRegistry registry ) {
333
+ if (!this .resourceProperties .isAddMappings ()) {
334
+ logger .debug ("Default resource handling disabled" );
335
+ return ;
336
+ }
337
+ addResourceHandler (registry , "/webjars/**" , "classpath:/META-INF/resources/webjars/" );
338
+ addResourceHandler (registry , this .mvcProperties .getStaticPathPattern (), (registration ) -> {
339
+ registration .addResourceLocations (this .resourceProperties .getStaticLocations ());
340
+ if (this .servletContext != null ) {
341
+ ServletContextResource resource = new ServletContextResource (this .servletContext , SERVLET_LOCATION );
342
+ registration .addResourceLocations (resource );
343
+ }
344
+ });
345
+ }
346
+
347
+ private void addResourceHandler (ResourceHandlerRegistry registry , String pattern , String ... locations ) {
348
+ addResourceHandler (registry , pattern , (registration ) -> registration .addResourceLocations (locations ));
349
+ }
350
+
351
+ private void addResourceHandler (ResourceHandlerRegistry registry , String pattern ,
352
+ Consumer <ResourceHandlerRegistration > customizer ) {
353
+ if (registry .hasMappingForPattern (pattern )) {
354
+ return ;
355
+ }
356
+ ResourceHandlerRegistration registration = registry .addResourceHandler (pattern );
357
+ customizer .accept (registration );
358
+ registration .setCachePeriod (getSeconds (this .resourceProperties .getCache ().getPeriod ()));
359
+ registration .setCacheControl (this .resourceProperties .getCache ().getCachecontrol ().toHttpCacheControl ());
360
+ registration .setUseLastModified (this .resourceProperties .getCache ().isUseLastModified ());
361
+ customizeResourceHandlerRegistration (registration );
362
+ }
363
+
364
+ private Integer getSeconds (Duration cachePeriod ) {
365
+ return (cachePeriod != null ) ? (int ) cachePeriod .getSeconds () : null ;
366
+ }
367
+
368
+ private void customizeResourceHandlerRegistration (ResourceHandlerRegistration registration ) {
369
+ if (this .resourceHandlerRegistrationCustomizer != null ) {
370
+ this .resourceHandlerRegistrationCustomizer .customize (registration );
371
+ }
372
+ }
373
+
315
374
@ Bean
316
375
@ ConditionalOnMissingBean ({ RequestContextListener .class , RequestContextFilter .class })
317
376
@ ConditionalOnMissingFilterBean (RequestContextFilter .class )
@@ -328,8 +387,6 @@ public static RequestContextFilter requestContextFilter() {
328
387
@ EnableConfigurationProperties (WebProperties .class )
329
388
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
330
389
331
- private static final Log logger = LogFactory .getLog (WebMvcConfigurer .class );
332
-
333
390
private final Resources resourceProperties ;
334
391
335
392
private final WebMvcProperties mvcProperties ;
@@ -340,8 +397,6 @@ public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfigurat
340
397
341
398
private final WebMvcRegistrations mvcRegistrations ;
342
399
343
- private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer ;
344
-
345
400
private ResourceLoader resourceLoader ;
346
401
347
402
@ SuppressWarnings ("deprecation" )
@@ -356,7 +411,6 @@ public EnableWebMvcConfiguration(
356
411
this .mvcProperties = mvcProperties ;
357
412
this .webProperties = webProperties ;
358
413
this .mvcRegistrations = mvcRegistrationsProvider .getIfUnique ();
359
- this .resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider .getIfAvailable ();
360
414
this .beanFactory = beanFactory ;
361
415
}
362
416
@@ -396,50 +450,6 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping(
396
450
resourceUrlProvider );
397
451
}
398
452
399
- @ Override
400
- protected void addResourceHandlers (ResourceHandlerRegistry registry ) {
401
- super .addResourceHandlers (registry );
402
- if (!this .resourceProperties .isAddMappings ()) {
403
- logger .debug ("Default resource handling disabled" );
404
- return ;
405
- }
406
- ServletContext servletContext = getServletContext ();
407
- addResourceHandler (registry , "/webjars/**" , "classpath:/META-INF/resources/webjars/" );
408
- addResourceHandler (registry , this .mvcProperties .getStaticPathPattern (), (registration ) -> {
409
- registration .addResourceLocations (this .resourceProperties .getStaticLocations ());
410
- if (servletContext != null ) {
411
- registration .addResourceLocations (new ServletContextResource (servletContext , SERVLET_LOCATION ));
412
- }
413
- });
414
- }
415
-
416
- private void addResourceHandler (ResourceHandlerRegistry registry , String pattern , String ... locations ) {
417
- addResourceHandler (registry , pattern , (registration ) -> registration .addResourceLocations (locations ));
418
- }
419
-
420
- private void addResourceHandler (ResourceHandlerRegistry registry , String pattern ,
421
- Consumer <ResourceHandlerRegistration > customizer ) {
422
- if (registry .hasMappingForPattern (pattern )) {
423
- return ;
424
- }
425
- ResourceHandlerRegistration registration = registry .addResourceHandler (pattern );
426
- customizer .accept (registration );
427
- registration .setCachePeriod (getSeconds (this .resourceProperties .getCache ().getPeriod ()));
428
- registration .setCacheControl (this .resourceProperties .getCache ().getCachecontrol ().toHttpCacheControl ());
429
- registration .setUseLastModified (this .resourceProperties .getCache ().isUseLastModified ());
430
- customizeResourceHandlerRegistration (registration );
431
- }
432
-
433
- private Integer getSeconds (Duration cachePeriod ) {
434
- return (cachePeriod != null ) ? (int ) cachePeriod .getSeconds () : null ;
435
- }
436
-
437
- private void customizeResourceHandlerRegistration (ResourceHandlerRegistration registration ) {
438
- if (this .resourceHandlerRegistrationCustomizer != null ) {
439
- this .resourceHandlerRegistrationCustomizer .customize (registration );
440
- }
441
- }
442
-
443
453
@ Bean
444
454
public WelcomePageHandlerMapping welcomePageHandlerMapping (ApplicationContext applicationContext ,
445
455
FormattingConversionService mvcConversionService , ResourceUrlProvider mvcResourceUrlProvider ) {
0 commit comments