SpringBoot静态资源配置原理
1. 配置类
首先,打开WebMvcAutoConfiguration类,其中的一个静态类WebMvcAutoConfigurationAdapter下有一个addResourceHandlers方法
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
2. webjars方式请求静态资源(了解即可,用得很少)
该段代码中第一个 if 条件判断是针对URL中输入webjars来访问静态资源的
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
其中 " webjars/** " 要求访问静态资源时须在URL中加入 webjars/** 才能访问,如下图
当我们以这种方式访问静态资源,SpringBoot就会从classpath:/META-INF/resources/webjars/
下找寻静态资源并返回给用户
以这种方式访问静态资源,前提条件是得向pom.xml中以依赖的形式导入静态资源,例如,导入jquery
<dependency><!--Jquery组件(前端)-->
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
3. 通过设置静态资源路径模式访问静态资源(重点)
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
分析上面这段代码
-
首先
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
是获得默认的静态资源的路径模式
如上图所示,从源码中可以看到默认是" / ** ",即直接从地址栏中输入静态资源名称即可访问,例如
http://localhost:8080/jquery.min.js
即可访问到对应的静态资源
此处的路径模式也可以修改(下文会说) -
紧接着下面 if 语句中的代码
registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())
就是配置静态资源映射的路径。
其中staticPathPattern
是上文提到的默认的获得静态资源的路径模式。
addResourceLocations
方法则是添加一个或多个资源位置以从其中提供静态内容,点开this.resourceProperties.getStaticLocations()
可发现所添加的资源位置即为classpath下的四个目录(下图上方红框所示)即我们在地址栏输入静态资源的文件名称,springboot会自动地去这些目录中寻找并返回给我们
-
当然,我们也可以设置静态资源的路径模式,使其不为 /**
只需在application.yml中自定义即可(这里我设为static)
spring:
mvc:
static-path-pattern: /static/**
这样设置后,如果再访问静态资源的话,路径前必须加上static,否则访问不到,例如
http://localhost:8080/static/jquery.min.js
注意:此处定义了新的路径模式,那么addResourceHandlers方法就会执行第一个if语句return返回,就不会再往下执行默认的两个访问方式了
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}