Spring Boot 配置静态资源

学习 Spring Boot 配置静态资源。

1 Spring MVC 配置静态资源

先来回顾下在 Spring MVC 中如何配置静态资源。使用 Spring MVC 时,静态资源会被拦截,需要添加额外的配置,一般在 spring-mvc.xml 中配置,如下:

<mvc:resources mapping="/favicon.ico" location="favicon.ico" />
<mvc:resources mapping="/static/**" location="/static/" />

2 Spring Boot 配置静态资源

2.1 默认位置

Spring Boot 项目中的静态资源最常见的位置在 src/main/resources/static 目录下,其实共有 5 个默认位置能放,重复的资源以优先级高的为准。如下(优先级: 1 > 2 > 3 > 4 > 5 ):

  1. classpath:/META-INF/resources/
  2. classpath:/resources/
  3. classpath:/static/
  4. classpath:/public/
  5. /

其中,/ 表示类似 webapp 目录,即 webapp 中的静态文件也可以直接访问。

如果在 src/main/resources/static 目录下有一个 1.png 的文件,那么访问路径是 http://127.0.0.1:8080/1.png ,不需要加 static 。类似 Spring MVC 中的配置 <mvc:resources mapping="/**" location="/static/"/> ,实际上系统会去 /static/1.png 目录下查找相关的文件。

2.2 源码解读

打开 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration ,找到了静态资源拦截的配置,如下:

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));
}
  1. this.mvcProperties.getStaticPathPattern() 返回 “/**” 。
  2. this.resourceProperties.getStaticLocations() 返回 4 个位置 “classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/” ,然后 WebMvcAutoConfiguration.getResourceLocations 又添加了 “/” ,这样总共就是上述 5 个位置。

2.3 自定义位置

上述 5 个是系统默认的位置,有 2 种办法可以实现自定义位置。

  1. 通过 application.properties 配置文件。
# 自定义配置静态资源的匹配规则和路径
# 定义请求 URL 规则
# spring.mvc.static-path-pattern=/**
# 定义资源位置
# spring.resources.static-locations=classpath:/cxy35/
  1. 通过 Java 代码。

新增配置类 WebMvcConfig, 如下:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    // 自定义配置静态资源的匹配规则和路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/cxy35/");
    }
}


扫码关注微信公众号 程序员35 ,获取最新技术干货,畅聊 #程序员的35,35的程序员# 。独立站点:https://cxy35.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值