问题描述:
springboot通过WebMvcAutoConfiguration默认加载资源路径失效
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/ 项目根目录下
问题分析:
失效的原因分析,首先要知道WebMvcAutoConfiguration配置类成立的条件,其中有一条@ConditionalOnMissingBean({WebMvcConfigurationSupport.class}),当如果项目中没有WebMvcConfigurationSupport时,该自动配置类才会生效,而我在网上查到的在spring5.0后废除了WebMvcConfigurerAdapter的两种解决方案就是
1.直接实现WebMvcConfigurer(官方推荐)
2.继承WebMvcConfigurationSupport,但这种的坏处就是很多的自动配置就会失效,必须自己写
说到这里,这个问题也就解决了,就是由于我之前采用的第二种方案,导致自动配置失效,所有静态资源加载失败,也就不能访问
1.png

2019-08-16 22:19:16.448 WARN 3812 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping for GET /asserts/js/bootstrap.min.js
同时我们可以开启springboot的debug模式,看看mvc模块是否生效

WebMvcAutoConfiguration:

  Did not match:
     - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) found beans of type 'org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport' myMvcConfig (OnBeanCondition)
  Matched:
     - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet', 'org.springframework.web.servlet.config.annotation.WebMvcConfigurer' (OnClassCondition)
     - found 'session' scope (OnWebApplicationCondition)

WebMvcAutoConfiguration.ResourceChainCustomizerConfiguration:

  Did not match:
     - @ConditionalOnEnabledResourceChain did not find class org.webjars.WebJarAssetLocator (OnEnabledResourceChainCondition)
     - Ancestor org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration did not match (ConditionEvaluationReport.AncestorsMatchedCondition)

WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.FaviconConfiguration:

  Did not match:
     - Ancestor org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration did not match (ConditionEvaluationReport.AncestorsMatchedCondition)
  Matched:
     - @ConditionalOnProperty (spring.mvc.favicon.enabled) matched (OnPropertyCondition)

很明显也可以看出来并没有生效
得出的结论就是,在没有特殊情况下,想保留springboot的自动配置特性(如果不自动配置,其实也没必要使用springboot框架,个人观点),就直接实现WebMvcConfigurer

多说一句:
在implements WebMvcConfigurer的时候发现了一个java8的一个特性

 default void configurePathMatch(PathMatchConfigurer configurer) {
    }

在使用default时,可以写一个方法体即使在接口中