用代码解析Springboot静态资源访问-创新互联

这篇文章主要用代码解析Springboot静态资源访问,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

创新互联科技有限公司专业互联网基础服务商,为您提供联通服务器托管高防服务器,成都IDC机房托管,成都主机托管等互联网服务。

springboot静态资源加载默认是从/static(或/public或/resources或/META-INF/resources) 目录下加载静态资源。

加载的优选级别:/META-INF/resources》/resources》/public》/static

静态资源的加载源码分析(WebMvcAutoConfiguration类)

首先从WebMvcAutoConfiguration.class自动配置类部分代码来看:

//添加静态资源规则
public void addResourceHandlers(ResourceHandlerRegistry registry) {
      if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
      } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        //webjars依赖映射规则
        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));
        }
				//本地配置的映射规则
        //this.resourceProperties.getStaticLocations() 从ResourceProperties中加载静态路径
        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
          this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations            this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

      }
    }

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前名称:用代码解析Springboot静态资源访问-创新互联
路径分享:https://www.cdcxhl.com/article36/dpgopg.html

成都网站建设公司_创新互联,为您提供手机网站建设虚拟主机网站建设小程序开发App开发网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作