springboot2版本无法加载静态资源问题解决

  

<强>前言
  

  

在学习springboot的过程中,发现无法引用静态资源。我使用的是springboot2.2.1版本。

  

追溯源码,终于解决。并记录下解决思路。

  

<强>默认加载路径
  

  

首先得知道springboot默认加载得资源路径是什么。

  

首先我们看WebMvcAutoConfiguration这个类。里面有一个方法叫做addResourceHandlers ()

        @ configuration (proxyBeanMethods=false)   @ConditionalOnWebApplication(类型=Type.SERVLET)   @ConditionalOnClass ({Servlet。类,DispatcherServlet。类,WebMvcConfigurer。类})   @ConditionalOnMissingBean (WebMvcConfigurationSupport.class)   @AutoConfigureOrder(命令。HIGHEST_PRECEDENCE + 10)   @AutoConfigureAfter ({DispatcherServletAutoConfiguration。类、TaskExecutionAutoConfiguration.class   ValidationAutoConfiguration。类})   公开课WebMvcAutoConfiguration {   @Override   公共空间addResourceHandlers (ResourceHandlerRegistry注册表){   如果(! this.resourceProperties.isAddMappings ()) {   logger.debug(“默认资源处理残疾”);   返回;   }   持续时间cachePeriod=this.resourceProperties.getCache () .getPeriod ();   .toHttpCacheControl .getCachecontrol CacheControl CacheControl=this.resourceProperties.getCache () () ();//所有/webjars/* *,都去类路径:/meta - inf/资源/webjars/找资源   如果(! registry.hasMappingForPattern (/webjars/* *)) {   customizeResourceHandlerRegistration (registry.addResourceHandler (“/webjars/* *”)   .addResourceLocations(“类路径:/meta - inf/资源/webjars/?   .setCachePeriod (getSeconds (cachePeriod) .setCacheControl (cacheControl));   }//静态资源文件夹映射   字符串staticPathPattern=this.mvcProperties.getStaticPathPattern ();   如果(! registry.hasMappingForPattern (staticPathPattern)) {   customizeResourceHandlerRegistration (registry.addResourceHandler (staticPathPattern)   .addResourceLocations (getResourceLocations (this.resourceProperties.getStaticLocations ()))   .setCachePeriod (getSeconds (cachePeriod) .setCacheControl (cacheControl));   }   }   }      

首先springboot会将我们类路径:/meta - inf/资源/webjars/路径下的文件映射为/webjars/* *

  

然后再一个如果判断进行静态资源文件夹映射,首先判断我们是否以使用“/* *”做映射

  

如果没有,则将“/* *”访问当前项目的任何资源,都去(如下静态资源的文件夹)找映射

        “类路径:/元公/正/资源/?   “类路径:/资源/?   “类路径:/静态/?   “类路径:/公共/?   “/?当前项目的根路径      

什么意思呢?举一个例子,就是说默认情况下我们假如我们调用http://localhost: 8080/a.json

  

Springboot就会从上面得这几个路径下去找a.json这个文件。

  

<强>问题所在
  

  

源码也是如同猜想得这样,那为什么我的代码中,直接访问静态资源却无法做映射呢?

  

我们再仔细看看WebMvcAutoConfiguration这个类。在其头上有一个这个注解:

  

@ConditionalOnMissingBean (WebMvcConfigurationSupport.class)

  

卧槽,瞬间恍然大悟。在我得配置文件中:

        @ configuration   公开课MyMVCConfig延伸WebMvcConfigurationSupport {   …   }      

继承了WebMvcConfigurationSupport这个类,使得springboot的自动装配失效了。因为@ConditionalOnMissingBean这个注解得作用就是,当容器中不存在这个类,如下得代码才有作用。

  

为什么会这样设计呢?

  

因为有时候我们得项目并不希望springboot给我们自动装配。希望完全由我们自己来配置自己来掌握。

  

要想达到这个效果,springboot给我们提供了一个更为简洁得方式。

        @Retention (RetentionPolicy.RUNTIME)   @Target (ElementType.TYPE)   @Documented   @ import (DelegatingWebMvcConfiguration.class)   公共@ interface EnableWebMvc {   }      

@EnableWebMvc注解会导入DelegatingWebMvcConfiguration。cls
  

  

而DelegatingWebMvcConfiguration又继承了WebMvcConfigurationSupport

        公开课DelegatingWebMvcConfiguration延伸WebMvcConfigurationSupport {      

所以当我们加上@EnableWebMvc也会有同样得效果且简洁。

  

<>强自定义配置资源映射
  

  

springboot当然也支持我们个性化得指定映射路径,我总结了如下几个方式:

springboot2版本无法加载静态资源问题解决