SpringBoot 2 系列八:Web开发——静态资源处理与请求映射原理分析


SpringBoot 官方文档:web开发

静态资源

默认静态资源目录

By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so that you can modify that behavior by adding your own WebMvcConfigurer and overriding the addResourceHandlers method.

默认情况下,SpringBoot应用的静态资源目录为 /resources/ 下的

  • /static
  • /public
  • /META-INF/resources
  • /resources
    只要将静态资源放在这些目录下,都能够访问到
    在这里插入图片描述
    http://127.0.0.1:8080/610.jpg
    http://127.0.0.1:8080/866.jpg
    http://127.0.0.1:8080/1059.jpg
    http://127.0.0.1:8080/237.jpg

静态资源统一前缀

By default, resources are mapped on /, but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/ can be achieved as follows:
默认情况下,资源映射到/**上,但是您可以使用spring.mvc来调整它。static-path-pattern属性。例如,将所有资源重新部署到/res/**可以实现如下:

 spring:
  mvc:
    static-path-pattern: "/res/**"

这样配置之后,所有的静态资源访问都得加上前缀 /res
http://127.0.0.1:8080/res/610.jpg
http://127.0.0.1:8080/res/866.jpg
http://127.0.0.1:8080/res/1059.jpg
http://127.0.0.1:8080/res/237.jpg

改变静态资源路径

默认的静态资源路径在类路径下的 /static (or /public or /resources or /META-INF/resources) 四个文件夹下,可以修改配置来改变静态资源路径
注意:旧版的SpringBoot 中的配置是 spring.resources.static-locations ,新版改为 spring.web.rersources.static-locations

spring:
  mvc:
    # 统一静态资源访问前缀
    static-path-pattern: "/res/**"
  web:
    resources:
      # 修改静态资源默认路径
      static-locations: ["classpath:/static/"]

欢迎页 welcome

Spring Boot同时支持静态和模板化欢迎页面。它首先在配置的静态内容位置中查找index.html文件。如果没有找到,则查找索引模板。如果找到其中任何一个,它将自动用作应用程序的欢迎页面。
在静态资源文件夹下创建一个 index.html ,springboot 就会将其作为欢迎页面,访问 http://127.0.0.1:8080/ 即可访问到页面,但是要注意的是,如果配置了静态资源访问前缀,欢迎页 index.html 在静态资源路径下,就不能直接访问到,需要加上访问前缀
在这里插入图片描述

网站图标 favicon

同欢迎页,在静态资源路径下,创建 favicon.ico 作用网站图标,同样会受到静态资源访问前缀配置的影响。

web 源码分析

SpringMvc 自动配置

核心 WebMvcAutoConfiguration。其中定义了一个自动配置适配器WebMvcAutoConfigurationAdapter
在这里插入图片描述两个关键属性:

private final ResourceProperties resourceProperties;

绑定配置文件:

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
private final WebMvcProperties mvcProperties;

绑定配置文件

@ConfigurationProperties(
    prefix = "spring.mvc"
)

WebMvcAutoConfigurationAdapter 这个适配器(配置类),只有一个有参构造器,所有的参数值都会从容器中获取

/**
 * ResourceProperties resourceProperties 获取配置文件中spring.resources下的所有属性
 * WebMvcProperties mvcProperties 获取配置文件中spring.mvc下的所有属性
 * ListableBeanFactory beanFactory 获取spring 的对象工厂
 */
public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider, ObjectProvider<WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider, ObjectProvider<DispatcherServletPath> dispatcherServletPath, ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
   
            this.resourceProperties = resourceProperties;
            this.mvcProperties = mvcProperties;
            this.beanFactory = beanFactory;
            this.messageConvertersProvider = messageConvertersProvider;
            this.resourceHandlerRegistrationCustomizer = (WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer)resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
            this.dispatcherServletPath = dispatcherServletPath;
            this.servletRegistrations = servletRegistrations;
        }

静态资源处理

  public void addResourceHandlers(ResourceHandlerRegistry registry) {
   
  			// 读取 resourceProperties.isAddMappings 属性值,默认为true 
            if (!this.resourceProperties.isAddMappings()) {
   
            	// 如果配置了false,就禁止了所有静态资源访问
                logger.debug("Default resource handling disabled");
            } else {
   
            		// 静态资源缓存 单位秒
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperti
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋水浮萍任缥缈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值