springboot中的资源访问路径
为什么写这篇呢?
先看下大致结构
有朋友问,为什么 a.js 直接就是 “ / ” 呢
不是 /static/a.js
我想大致原因如下
找啊找啊,找到了 ResourceProperties
看看它做了什么吧
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
//静态资源的默认的加载位置,而且是私有不可更改的
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations; //构造时就等于CLASSPATH_RESOURCE_LOCATIONS
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
this.addMappings = true;
this.chain = new ResourceProperties.Chain();
this.cache = new ResourceProperties.Cache();
}
public String[] getStaticLocations() {
return this.staticLocations;
}
而我们的发布目录结构是这样的
直接请求 a.js ,他会在CLASSPATH_RESOURCE_LOCATIONS 取找,所以是直接可以用 “ / ” 访问的。如果是动态资源就不一样了哦