1. 默认配置 (零配置)
在不做任何额外配置的情况下,Spring Boot 会自动从以下 4 个 classpath 路径下查找并提供静态资源。这些路径有优先级顺序,如果不同路径下有同名文件,会按照以下顺序查找,找到第一个即返回:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/ (最常用)
- classpath:/public/
什么是 classpath?
在标准的 Maven/Gradle 项目结构中,classpath 主要指向 src/main/resources 目录。因此,你只需要在你的项目中创建上述任意一个文件夹,并将静态资源(如 HTML, CSS, JavaScript, 图片等)放入其中即可。
示例:
假设你的项目结构如下:
src
└── main
├── java
│ └── com
│ └── example
│ └── myapp
│ └── MyApplication.java
└── resources
└── static
├── index.html
├── css
│ └── style.css
└── js
└── main.js
当你启动 Spring Boot 应用后,可以直接通过浏览器访问这些资源:
- http://localhost:8080/index.html
- http://localhost:8080/css/style.css
- http://localhost:8080/js/main.js
注意:index.html 是一个特殊文件。如果 static 目录下有 index.html,那么访问应用的根路径 http://localhost:8080/ 会自动展示这个页面。
2. 自定义配置
如果你不希望使用默认的路径,或者有更复杂的映射需求,可以通过以下两种方式进行自定义。
方式一:通过application.properties或application.yml
这是最简单的自定义方式。
你可以通过
spring.web.resources.static-locations 属性来覆盖默认的静态资源路径。
重要提示:此配置会完全替换掉默认的 4 个路径,而不是在它们的基础上增加。
使用 application.yml:
spring:
web:
resources:
# 将静态资源路径指定为 classpath 下的 my-static 文件夹
static-locations: classpath:/my-static/
现在,Spring Boot 只会从
src/main/resources/my-static/ 目录加载静态资源。
如果你想添加一个新路径,同时保留默认路径,需要把默认路径也写上:
spring:
web:
resources:
static-locations:
- classpath:/my-static/ # 你的自定义路径
- classpath:/META-INF/resources/ # 以下是默认路径
- classpath:/resources/
- classpath:/static/
- classpath:/public/
从文件系统加载:你也可以从项目外部的文件系统加载资源,使用 file: 协议。
spring:
web:
resources:
# 从 D盘的 my-files 目录加载
static-locations: file:D:/my-files/
默认情况下,静态资源是从根路径 / 开始访问的。你可以通过
spring.mvc.static-path-pattern 来修改这个前缀。
使用 application.yml:
spring:
mvc:
# 为所有静态资源添加 /assets 的URL前缀
static-path-pattern: /assets/**
配置后,原来的访问路径会变成:
- http://localhost:8080/index.html -> http://localhost:8080/assets/index.html
- http://localhost:8080/css/style.css -> http://localhost:8080/assets/css/style.css
方式二:通过 Java 配置类 (WebMvcConfigurer)
这种方式提供了最大的灵活性,适用于复杂的映射规则或需要多个不同映射关系的场景。你需要创建一个配置类并实现 WebMvcConfigurer 接口。
与属性配置不同,使用 Java 配置类是添加新的映射规则,不会覆盖 Spring Boot 的默认配置。
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 MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 添加一个新的资源处理器
registry
// 1. addResourceHandler: 定义 URL 访问路径,/my-res/** 表示 /my-res/ 下的所有路径
.addResourceHandler("/my-res/**")
// 2. addResourceLocations: 定义了文件存放的物理位置或 classpath 位置
// 可以指定多个位置,会按顺序查找
// 注意:路径末尾的斜杠 / 很重要!
.addResourceLocations("classpath:/custom-static/", "file:E:/external-res/");
}
}
代码解释:
- addResourceHandler("/my-res/**"):
- 这个方法指定了 URL 的匹配模式。
- 当浏览器请求的 URL 以 /my-res/ 开头时(例如 /my-res/logo.png),这个处理器就会生效。
- addResourceLocations("classpath:/custom-static/", "file:E:/external-res/"):
- 这个方法指定了静态资源在项目中的实际位置。
- classpath:/custom-static/ 指的是 src/main/resources/custom-static/ 目录。
- file:E:/external-res/ 指的是 E 盘下的 external-res 目录。
- 当请求 /my-res/logo.png 时,Spring Boot 会先去 src/main/resources/custom-static/ 目录下找 logo.png,如果找不到,会再去 E:/external-res/ 目录下找 logo.png。
3. 缓存配置 (性能优化)
为了提升性能,可以为静态资源配置 HTTP 缓存。
使用 application.yml:
spring:
web:
resources:
cache:
# 缓存周期,例如 365 天
period: P365D # ISO 8601 Duration format
# 控制 Cache-Control HTTP header
cachecontrol:
max-age: 365d
cache-public: true # 或者 cache-private
# no-cache: true # 如果需要
总结与最佳实践
场景 | 推荐方法 |
简单项目,默认即可 | 在 |
需要更改默认存放目录 | 在 application.yml 中配置 |
需要为所有静态资源加统一前缀 | 在 application.yml 中配置 |
复杂的、多个映射规则 | 实现 WebMvcConfigurer 接口,使用 addResourceHandlers 方法。 |
管理前端库 | 使用 WebJars,通过 Maven/Gradle 管理依赖。 |
生产环境性能优化 | 在 application.yml 中配置 |
以上就是 Spring Boot 3.x 中关于静态资源配置的全面指南。通常情况下,默认配置已经能满足绝大部分需求。