当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot静态资源访问实现代码解析

Springboot静态资源访问实现代码解析

2020年06月23日  | 移动技术网IT编程  | 我要评论

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));
        }

      }
    }

resourceproperties类部分源码

@configurationproperties(
  prefix = "spring.resources",
  ignoreunknownfields = false
)
public class resourceproperties {
  //springboot默认的加载路径
  private static final string[] classpath_resource_locations = new string[]{"classpath:/meta-inf/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
  private string[] staticlocations;
  private boolean addmappings;
  private final resourceproperties.chain chain;
  private final resourceproperties.cache cache;

映射规则总结

在springboot静态资源加载规则中,除了”标准“静态资源位置之外,还有一个较为特殊的webjars

“标准”静态资源映射规则

​所有的“/**”的请求,如果没有对应的处理,那么就去默认映射的静态资源目录下去找,如下所示:

  1. "classpath:/meta-inf/resources/"
  2. "classpath:/resources/"
  3. "classpath:/static/",
  4. "classpath:/public/"
  5. “/**”

所有的webjars的请求都会去 ”classpath:/meta-inf/resources/webjars/**“去资源

(如果 以jar包的方式来引入jquery包)

在pom.xml中引入依赖

 <dependency>
      <groupid>org.webjars</groupid>
      <artifactid>jquery</artifactid>
      <version>3.3.1-2</version>
    </dependency>

从引入的包目录来看

springboot默认欢迎页面

自动去加载默认目录下的;如static/

自定义配置静态资源目录

在application.properties文件中去配置

//配置test为静态资源目录
spring.resources.static-locations=classpath:/test/

遇到的坑

在配置了静态资源目录的时候,跳转到的页面路径不能写绝对路径,

比如:spring.resources.static-locations=classpath:/test/ 我配置test为静态资源的加载位置,在访问的时候不需要写test

请求:http://127.0.0.1:8085/test/1.png

请求:http://127.0.0.1:8085/1.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网