目录
点睛
1 在Springboot,我们可以使用以下方式处理静态资源
webjars:映射 localhost:8080/webjars/
public,static,/**,resources 映射 localhost:8080/
2 优先级:resources>static(默认)>public
3 webjars
说明: http://www.mamicode.com/info-detail-2081046.html
关键源码
1 资源加载的位置(ResourceProperties)
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
2 加载资源源代码(WebMvcAutoConfiguration)
@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));
}
}
访问webjar中的静态资源

本文详细介绍了在SpringBoot项目中如何处理和访问WebJars中的静态资源,包括资源映射、加载位置及优先级设置。通过具体源码解析,了解WebMvcAutoConfiguration如何实现资源加载。


被折叠的 条评论
为什么被折叠?



