SpringBoot 对静态资源的映射规则

1. SpringBoot 对静态资源的映射规则

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
	//ResourceProperties 可以设置和资源有关的参数,缓存时间等
    if(!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        if(!registry.hasMappingForPattern("/webjars/**")) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }
		// staticPathPattern=/**,registry添加资源映射,路径/** 
        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if(!registry.hasMappingForPattern(staticPathPattern)) {
			this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}
  • 所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源
    webjars:以jar包的方式引入静态资源
    www.webjars.org
    在这里插入图片描述
    在这里插入图片描述

所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源
http://127.0.0.1:8080/boot/webjars/jquery/3.4.1/jquery.js 可访问到jar包中的jquery.js
在访问的时候只需写webjars下面的资源名称即可

@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/"};
}

ResourceProperties 可以设置和静态资源有关的参数,缓存时间等

i. 所有/webjars/**,都去classpath:META_INF/resource/webjars/找资源
ii. 添加资源映射,路径/**,访问当前项目的任何资源

...
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
// staticPathPattern =/**
...
// staticPathPattern =/**
// this.resourceProperties.getStaticLocations() => 
// => staticLocations = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
//getResourceLocations(staticLocations) => 
registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
...
// => 复制完得到 {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"}
static String[] getResourceLocations(String[] staticLocations) {
    String[] locations = new String[staticLocations.length + WebMvcAutoConfiguration.SERVLET_LOCATIONS.length];
    System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length);
    System.arraycopy(WebMvcAutoConfiguration.SERVLET_LOCATIONS, 0, locations, staticLocations.length, WebMvcAutoConfiguration.SERVLET_LOCATIONS.length);
    return locations;
}

经过上面的方法,添加映射路径/**,访问任何资源,如果没人处理,就到 {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"} 下寻找资源

iii. 欢迎页配置:静态资源文件夹下所有index.html,被/**映射

//配置欢迎页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
    return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}

iv. 所有 **/favicon.ico 都是在静态资源文件夹下找

@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
    SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
    mapping.setOrder(-2147483647);
    mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
    return mapping;
}

v. 自定义静态文件夹位置

spring.resources.static-locations=classpath:/hello/, classpath:/world/
发布了156 篇原创文章 · 获赞 4 · 访问量 7936
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览