前言

很多的小伙伴刚刚接触SpringBoot的时候,可能会遇到加载不到静态资源的情况。

比如html没有样式,图片无法加载等等。

今天王子就与大家一起看看SpringBoot中关于资源映射部分的主要源码实现。

建议环境允许的情况下,小伙伴们自己使用idea创建一个springBoot项目,跟着文章和王子一起看一看源码,更容易理解。

SSM中的资源映射

在谈SpringBoot之前,我们先回顾一下SSM中关于资源配置是如何实现的。

在SSM环境下,一般我们可以通过<mvc:resources />节点来配置不拦截静态资源,就像下边这样:

<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/img/**" location="/img/" />

这里的/**表示的是可以匹配任意层级的路径,属于ant风格表达式,感兴趣的小伙伴自行百度了解即可,所以也可以写成下面这样

<mvc:resources mapping="/**" location="/" />

上边的这种配置方式是属于XML配置的方式,SpringMVC的配置方式除了XML配置,也是可以通过java代码配置的,只需要我们自己定义一个类,来继WebMvcConfigurationSupport这个类即可,我们看一下WebMvcConfigurationSupport的源码部分:

    /**
* Override this method to add resource handlers for serving static resources.
* @see ResourceHandlerRegistry
*/
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
}

上边的注释写的很清楚,重写这个方法来增加一个静态资源的映射。那么具体怎么写呢?

我们再按照注释看一下ResourceHandlerRegistry的源码,重点看一下对这个类的注释,如下。

 /* <p>To create a resource handler, use {@link #addResourceHandler(String...)} providing the URL path patterns
/ * for which the handler should be invoked to serve static resources (e.g. {@code "/resources/**"}).

大概意思就是为了创建资源的处理器,要调用addResourceHandler方法来提供url的表达式,这个方法是为了服务静态资源的(ps:王子的英语水平也一般,了解大意即可)

然后我们去看addResourceHandler方法:

    /**
* Add a resource handler for serving static resources based on the specified URL path patterns.
* The handler will be invoked for every incoming request that matches to one of the specified
* path patterns.
* <p>Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"} are allowed.
* See {@link org.springframework.util.AntPathMatcher} for more details on the syntax.
* @return a {@link ResourceHandlerRegistration} to use to further configure the
* registered resource handler
*/
public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) {
ResourceHandlerRegistration registration = new ResourceHandlerRegistration(pathPatterns);
this.registrations.add(registration);
return registration;
}

我们看到这个方法执行后返回了一个新的类ResourceHandlerRegistration

那我们再来看一下这个类中的核心方法

    /**
* Add one or more resource locations from which to serve static content.
* Each location must point to a valid directory. Multiple locations may
* be specified as a comma-separated list, and the locations will be checked
* for a given resource in the order specified.
* <p>For example, {{@code "/"}, {@code "classpath:/META-INF/public-web-resources/"}}
* allows resources to be served both from the web application root and
* from any JAR on the classpath that contains a
* {@code /META-INF/public-web-resources/} directory, with resources in the
* web application root taking precedence.
* <p>For {@link org.springframework.core.io.UrlResource URL-based resources}
* (e.g. files, HTTP URLs, etc) this method supports a special prefix to
* indicate the charset associated with the URL so that relative paths
* appended to it can be encoded correctly, e.g.
* {@code [charset=Windows-31J]https://example.org/path}.
* @return the same {@link ResourceHandlerRegistration} instance, for
* chained method invocation
*/
public ResourceHandlerRegistration addResourceLocations(String... resourceLocations) {
this.locationValues.addAll(Arrays.asList(resourceLocations));
return this;
}

上边注释的大意就是增加一个或者多个静态资源路径,并举了一些例子。源码我们就看到这里。

所以我们可以像这样实现资源的映射:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /**
* @author liumeng
* @Date: 2020/9/25 09:20
* @Description:
*/
@Configuration
public class SpringMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}

关于SpringMVC中的资源映射部分就介绍到这,那么我们继续来看SpringBoot的资源映射吧。

SpringBoot的资源映射

其实SpringBoot的资源映射也是一脉相承的,当我们初始化一个SpringBoot项目后,静态资源会默认存在resource/static目录中,那么SpringBoot的底层是怎么实现的呢,接下来我们就去源码里探索一下。

SpringBoot的源码在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));
}
}

这里面我们重点看下边这部分

       String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}

我们看一下getStaticPathPattern()方法的实现,发现获得的就是

    /**
* Path pattern used for static resources.
*/
private String staticPathPattern = "/**";

这个属性的值,默认是/**。

然后我们再看this.resourceProperties.getStaticLocations()方法,发现获得的是

    private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

而CLASSPATH_RESOURCE_LOCATIONS是一个常量,值如下:

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

在这里我们看到了四个值,static就是其中一个,到这里我们就明白了SpringBoot的静态资源为什么会存在resource/static这个目录下,而且放在以上4个目录中是都可以读取到的。

实际上SpringBoot默认的静态资源是5个,我们再来看getResourceLocations方法,如下:

    static String[] getResourceLocations(String[] staticLocations) {
String[] locations = new String[staticLocations.length + SERVLET_LOCATIONS.length];
System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length);
System.arraycopy(SERVLET_LOCATIONS, 0, locations, staticLocations.length, SERVLET_LOCATIONS.length);
return locations;
}

可以看到,除了我们之前看到的4个路径,这个方法里还新增了一个SERVLET_LOCATIONS的路径,点进去看一下

    private static final String[] SERVLET_LOCATIONS = { "/" };

发现就是一个"/"。

所以实际上SpringBoot的默认静态资源路径有5个:

"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/",“/”

自定义配置

好了,到现在我们已经知道了SpringBoot的默认资源映射来源,那么我们如何配置自定义的资源映射路径呢?

其实我们可以直接通过application.properties配置,如下:

spring.resources.static-locations=classpath:/
spring.mvc.static-path-pattern=/**

那么为什么这样配置就可以了呢,是因为相应的类上使用了下面的注解:

@ConfigurationProperties(prefix = "spring.mvc")

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)

本文就不对这个注解再做深入研究了。感兴趣的小伙伴可以持续关注我们的后续文章。

除了通过配置文件自定义,还可以通过java代码进行配置,这种方式和我们上文说到的SpringMvc方式比较类似,只不过我们这次是实现的WebMvcConfigurer这个接口

实现方式如下:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration
public class ResourcesConfig implements WebMvcConfigurer
{ @Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler( "/**").addResourceLocations("classpath:/aaa");
}
}

有了上边文章的铺垫,相信大家对于这段代码应该秒懂了吧。

到这里,小伙伴们是否会有个疑问,WebMvcConfigurer和WebMvcConfigurationSupport有什么关系呢?

这个问题本文就不再探索了,留给大家自行探索。

总结

好了,今天王子和大家一起从SpringMVC的源码开始探索,引出了SpringBoot的资源映射配置原理。

又介绍了SpringBoot自定义资源映射路径的两种方式,相信小伙伴们会有一个比较深刻的印象了。

本文到这里就结束了,如果觉得内容对你有所帮助,那么欢迎持续关注后续文章。

往期文章推荐:

Windows下使用Nginx+Tomcat做负载均衡

聊聊分布式下的WebSocket解决方案

从SpringBoot源码看资源映射原理的更多相关文章

  1. 带着萌新看springboot源码12(启动原理 下)

    先继续接上一篇,那个启动原理还有一点没说完. 6. afterRefresh(context, applicationArguments); 看这个名字就知道,应该就是ioc容器刷新之后的一些操作了, ...

  2. Dubbo源码学习--优雅停机原理及在SpringBoot中遇到的问题

    Dubbo源码学习--优雅停机原理及在SpringBoot中遇到的问题 相关文章: Dubbo源码学习文章目录 前言 主要是前一阵子换了工作,第一个任务就是解决目前团队在 Dubbo 停机时产生的问题 ...

  3. SpringBoot源码学习系列之启动原理简介

    本博客通过debug方式简单跟一下Springboot application启动的源码,Springboot的启动源码是比较复杂的,本博客只是简单梳理一下源码,浅析其原理 为了方便跟源码,先找个Ap ...

  4. 带着萌新看springboot源码8(spring ioc源码 完)

    上一节说到实例化了所有的单实例Bean,后面还有一步遍历 12.完成容器刷新(finishRefresh();) 那个和生命周期有关的后置处理器类型是LifecycleProcessor:监听器原理我 ...

  5. 从微信小程序开发者工具源码看实现原理(一)- - 小程序架构设计

    使用微信小程序开发已经很长时间了,对小程序开发已经相当熟练了:但是作为一名对技术有追求的前端开发,仅仅熟练掌握小程序的开发感觉还是不够的,我们应该更进一步的去理解其背后实现的原理以及对应的考量,这可能 ...

  6. 从SpringBoot源码分析 配置文件的加载原理和优先级

    本文从SpringBoot源码分析 配置文件的加载原理和配置文件的优先级     跟入源码之前,先提一个问题:   SpringBoot 既可以加载指定目录下的配置文件获取配置项,也可以通过启动参数( ...

  7. 从微信小程序开发者工具源码看实现原理(四)- - 自适应布局

    从前面从微信小程序开发者工具源码看实现原理(一)- - 小程序架构设计可以知道,小程序大部分是通过web技术进行渲染的,也就是最终通过浏览器的dom tree + cssom来生成渲染树:既然最终是通 ...

  8. SpringBoot源码学习系列之异常处理自动配置

    SpringBoot源码学习系列之异常处理自动配置 1.源码学习 先给个SpringBoot中的异常例子,假如访问一个错误链接,让其返回404页面 在浏览器访问: 而在其它的客户端软件,比如postm ...

  9. 助力SpringBoot自动配置的条件注解ConditionalOnXXX分析--SpringBoot源码(三)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 如何分析SpringBoot源码模块及结构?--SpringBoot源码(二) 上一篇分析了SpringBoo ...

随机推荐

  1. iptables初步接触

    0.iptables命令选项输入顺序:iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网 ...

  2. 月历输出php代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 【转】在Python的struct模块中进行数据格式转换的方法

    这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...

  4. 10分钟搞定 Java 并发队列好吗?好的

    | 好看请赞,养成习惯 你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it ...

  5. #企业项目实战 .Net Core + Vue/Angular 分库分表日志系统六 | 最终篇-通过AOP自动连接数据库-完成日志业务

    教程预览 01 | 前言 02 | 简单的分库分表设计 03 | 控制反转搭配简单业务 04 | 强化设计方案 05 | 完善业务自动创建数据库 06 | 最终篇-通过AOP自动连接数据库-完成日志业 ...

  6. 2020.08.14小bug

    页面下面的滚动条怎么清除 css overflow-x: hidden;

  7. 我是怎样刚拿饿了么P7 offer,然后途虎一轮游的

    今年初拿了个饿了么P7的offer,于此同时大家顺便看看我怎么途虎一轮游的.废话不多说,直接上题吧. 一面 首先上来就是自我介绍,简单的说下自己的项目经验,涉及的技术栈之类的. 然后每一轮必问的问题来 ...

  8. DHCP和NAT

    DHCP(dynamic host configuration protocol)用于内网动态分配IP,是一种基于UDP的应用层协议. NAT(net address translation)用于内网 ...

  9. openresty(nginx+lua)初识

    1.新增项目配置文件: vim /usr/example/example1.conf --将以下内容加入example1.conf server { listen 80; server_name _; ...

  10. C# 压缩源文件(导出源文件word文件)

    说明 1 : 在webUI 公共类,存放 ZipHelper 类 说明  2 :判断文件路径是否存在,不存在则创建文件夹 说明 3  : 引用类方法,判断压缩文件,返回的,是true/false 引用 ...