8484import org .springframework .web .accept .ContentNegotiationManager ;
8585import org .springframework .web .accept .ContentNegotiationStrategy ;
8686import org .springframework .web .bind .support .ConfigurableWebBindingInitializer ;
87+ import org .springframework .web .context .ServletContextAware ;
8788import org .springframework .web .context .request .NativeWebRequest ;
8889import org .springframework .web .context .request .RequestAttributes ;
8990import org .springframework .web .context .request .RequestContextListener ;
@@ -182,7 +183,11 @@ public OrderedFormContentFilter formContentFilter() {
182183 @ EnableConfigurationProperties ({ WebMvcProperties .class ,
183184 org .springframework .boot .autoconfigure .web .ResourceProperties .class , WebProperties .class })
184185 @ 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 ;
186191
187192 private final WebMvcProperties mvcProperties ;
188193
@@ -194,13 +199,19 @@ public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
194199
195200 private final ObjectProvider <ServletRegistrationBean <?>> servletRegistrations ;
196201
197- final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer ;
202+ private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer ;
203+
204+ private ServletContext servletContext ;
198205
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 ,
201210 ObjectProvider <ResourceHandlerRegistrationCustomizer > resourceHandlerRegistrationCustomizerProvider ,
202211 ObjectProvider <DispatcherServletPath > dispatcherServletPath ,
203212 ObjectProvider <ServletRegistrationBean <?>> servletRegistrations ) {
213+ this .resourceProperties = resourceProperties .hasBeenCustomized () ? resourceProperties
214+ : webProperties .getResources ();
204215 this .mvcProperties = mvcProperties ;
205216 this .beanFactory = beanFactory ;
206217 this .messageConvertersProvider = messageConvertersProvider ;
@@ -210,6 +221,11 @@ public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcPropert
210221 this .mvcProperties .checkConfiguration ();
211222 }
212223
224+ @ Override
225+ public void setServletContext (ServletContext servletContext ) {
226+ this .servletContext = servletContext ;
227+ }
228+
213229 @ Override
214230 public void configureMessageConverters (List <HttpMessageConverter <?>> converters ) {
215231 this .messageConvertersProvider
@@ -312,6 +328,49 @@ public void addFormatters(FormatterRegistry registry) {
312328 ApplicationConversionService .addBeans (registry , this .beanFactory );
313329 }
314330
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+
315374 @ Bean
316375 @ ConditionalOnMissingBean ({ RequestContextListener .class , RequestContextFilter .class })
317376 @ ConditionalOnMissingFilterBean (RequestContextFilter .class )
@@ -328,8 +387,6 @@ public static RequestContextFilter requestContextFilter() {
328387 @ EnableConfigurationProperties (WebProperties .class )
329388 public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
330389
331- private static final Log logger = LogFactory .getLog (WebMvcConfigurer .class );
332-
333390 private final Resources resourceProperties ;
334391
335392 private final WebMvcProperties mvcProperties ;
@@ -340,8 +397,6 @@ public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfigurat
340397
341398 private final WebMvcRegistrations mvcRegistrations ;
342399
343- private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer ;
344-
345400 private ResourceLoader resourceLoader ;
346401
347402 @ SuppressWarnings ("deprecation" )
@@ -356,7 +411,6 @@ public EnableWebMvcConfiguration(
356411 this .mvcProperties = mvcProperties ;
357412 this .webProperties = webProperties ;
358413 this .mvcRegistrations = mvcRegistrationsProvider .getIfUnique ();
359- this .resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider .getIfAvailable ();
360414 this .beanFactory = beanFactory ;
361415 }
362416
@@ -396,50 +450,6 @@ public RequestMappingHandlerMapping requestMappingHandlerMapping(
396450 resourceUrlProvider );
397451 }
398452
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-
443453 @ Bean
444454 public WelcomePageHandlerMapping welcomePageHandlerMapping (ApplicationContext applicationContext ,
445455 FormattingConversionService mvcConversionService , ResourceUrlProvider mvcResourceUrlProvider ) {
0 commit comments