栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot中的静态资源访问的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot中的静态资源访问的实现

一、说在前面的话

我们之间介绍过SpringBoot自动配置的原理,基本上是如下:

xxxxAutoConfiguration:帮我们给容器中自动配置组件;
xxxxProperties:配置类来封装配置文件的内容;

二、静态资源映射规则

1、对哪些目录映射?

classpath:/meta-INF/resources/ 
classpath:/resources/
classpath:/static/ 
classpath:/public/
/:当前项目的根路径

2、什么意思?

就我们在上面五个目录下放静态资源(比如:a.js等),可以直接访问(http://localhost:8080/a.js),类似于以前web项目的webapp下;放到其他目录下无法被访问。

3、为什么是那几个目录?

3.1、看源码

我们一起来读下源码,这个是SpringBoot自动配置的WebMvcAutoConfiguration.java类来帮我们干的。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  if (!this.resourceProperties.isAddMappings()) {
    logger.debug("Default resource handling disabled");
    return;
  }
  Integer cachePeriod = this.resourceProperties.getCachePeriod();
  if (!registry.hasMappingForPattern("/webjarsfavicon.ico",
      faviconRequestHandler()));
   return mapping;
  }

  @Bean
  public ResourceHttpRequestHandler faviconRequestHandler() {
   ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
   requestHandler
      .setLocations(this.resourceProperties.getFaviconLocations());
   return requestHandler;
  }

}

5.2、分析源码

// 首先可以看到的是可以设置是否生效,通过参数spring.mvc.favicon.enabled来配置,若无此参数,则默认是生效的。
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
========》
// 可以看到所有的**/favicon.ico都是在faviconRequestHandler()这个方法里找。
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
========》
faviconRequestHandler().this.resourceProperties.getFaviconLocations()
// 就是之前的五个静态资源文件夹。  
List getFaviconLocations() {
  List locations = new ArrayList(
      this.staticLocations.length + 1);
  if (this.resourceLoader != null) {
    for (String location : this.staticLocations) {
      locations.add(this.resourceLoader.getResource(location));
    }
  }
  locations.add(new ClassPathResource("/"));
  return Collections.unmodifiableList(locations);
}

5.3、一句话概括

只要把favicon.ico放到如下目录下,就会自动生效。

classpath:/meta-INF/resources/ 
classpath:/resources/
classpath:/static/ 
classpath:/public/
/:当前项目的根路径

6、webjars

6.1、看源码

WebMvcAutoConfiguration

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
  if (!this.resourceProperties.isAddMappings()) {
    logger.debug("Default resource handling disabled");
    return;
  }
  Integer cachePeriod = this.resourceProperties.getCachePeriod();
  if (!registry.hasMappingForPattern("/webjars/**")) {
    customizeResourceHandlerRegistration(
 registry.addResourceHandler("/webjars/**")
     .addResourceLocations(
  "classpath:/meta-INF/resources/webjars/")
 .setCachePeriod(cachePeriod));
  }
  String staticPathPattern = this.mvcProperties.getStaticPathPattern();
  if (!registry.hasMappingForPattern(staticPathPattern)) {
    customizeResourceHandlerRegistration(
 registry.addResourceHandler(staticPathPattern)
     .addResourceLocations(
  this.resourceProperties.getStaticLocations())
 .setCachePeriod(cachePeriod));
  }
}

6.2、分析源码

这次我们来分析前半截。

Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
  customizeResourceHandlerRegistration(
      registry.addResourceHandler("/webjars/**")
   .addResourceLocations(
"classpath:/meta-INF/resources/webjars/")
      .setCachePeriod(cachePeriod));
}

6.3、一句话概括

所有/webjars/**都从classpath:/meta-INF/resources/webjars/路径下去找对应的静态资源。

6.4、什么是webjars?

就是以jar包的方式引入静态资源。

官网地址:http://www.webjars.org/。类似于maven仓库。

我们可以做个例子,将jquery引入到项目中


  org.webjars
  jquery
  3.3.1

看项目依赖

会自动为我们引入jquery,要怎么使用呢?我们上面说过:

所有/webjars/*都从classpath:/meta-INF/resources/webjars/路径下去找对应的静态资源。

所以我们启动项目,访问:http://localhost:8080/webjars/jquery/3.3.1/jquery.js即可。

必须在这几个路径下SpringBoot才会扫描到!

"classpath:/meta-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

到此这篇关于SpringBoot中的静态资源访问的实现的文章就介绍到这了,更多相关SpringBoot 静态资源访问内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131802.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号