核心内容摘要
Excel规划求解全攻略:不懂编程也能做线性优化(含供应链案例)
Spring Boot 静态资源管理详解2026 年视角基于 Spring Boot
xSpring Boot 对静态资源CSS、JS、图片、字体、HTML 等的处理非常友好默认配置已经覆盖了绝大多数场景但自定义需求也很常见。
下面从默认规则 → 自定义映射 → WebJars一条龙讲清楚。
默认静态资源处理规则Spring Boot 自动配置Spring Boot 会自动注册一个ResourceHttpRequestHandler处理所有未被 Controller 匹配的请求通常是 /** 路径。
默认查找顺序classpath 优先级从高到低classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/额外特殊规则/webjars/**→ 自动映射到 classpath 中所有 WebJars 的/META-INF/resources/webjars/详见
分常见目录对应示例项目结构src/main/resources/ ├── static/ ← 最常用放 css/js/img │ ├── css/ │ │ └── style.css │ ├── js/ │ └── img/ ├── public/ ← 放 index.html 等欢迎页优先级较低 ├── resources/ ← 较少用 └── META-INF/resources/ ← 第三方 jar 常用访问示例假设应用跑在 http://localhost:8080/css/style.css→ 查找 classpath:/static/css/style.css/img/logo.png→ 查找 classpath:/static/img/logo.png/index.html→ 如果存在 public/index.html 或 static/index.html会作为欢迎页默认 URL 模式/**所有路径都尝试匹配静态资源配置文件方式修改默认路径application.yml / properties# 完全替换默认位置逗号分隔多个spring:web:resources:static-locations:-classpath:/my-static/-file:/opt/extra-files/# 外部目录生产常用# 只改 URL 前缀默认 /** 改成 /assets/**spring:mvc:static-path-pattern:/assets/**注意如果用了static-path-pattern/assets/**那么原来/css/style.css就访问不到了要改成/assets/css/style.css。
自定义静态资源映射最灵活方式当默认位置不够用时比如多模块项目、外部目录、特定前缀、缓存控制推荐实现WebMvcConfigurer。
importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;ConfigurationpublicclassWebConfigimplementsWebMvcConfigurer{OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){// 自定义路径 /files/** → 映射到 classpath:/upload/registry.addResourceHandler(/files/**).addResourceLocations(classpath:/upload/)// classpath.addResourceLocations(file:/data/app/upload/)// 外部磁盘Linux/Windows 路径.setCachePeriod(
// 缓存 1 小时秒.resourceChain(true);// 开启链式解析可加版本控制等// 额外一个/cdn/** → 外部 CDN 或另一个目录registry.addResourceHandler(/cdn/**).addResourceLocations(file:/opt/cdn-static/);// 如果你想保留默认静态资源可以不覆盖 /**Spring Boot 会自动追加// 但如果你覆盖了 /**记得手动加上默认位置registry.addResourceHandler(/**).addResourceLocations(classpath:/static/,classpath:/public/,classpath:/resources/,classpath:/META-INF/resources/);}}高级玩法加版本控制防缓存.addResourceResolver(newVersionResourceResolver().addContentVersionStrategy(/**))// 根据文件内容 MD5 加版本或使用FixedVersionStrategy固定版本号。
WebJars前端依赖管理神器WebJars 把 npm/bower 等前端库打包成 Maven/Gradle 依赖自动放到 classpath。
默认支持Spring Boot 内置无需额外配置路径/webjars/**映射到所有 classpath 中/META-INF/resources/webjars/xxx/添加依赖示例pom.xml!-- Bootstrap 5 --dependencygroupIdorg.webjars/groupIdartifactIdbootstrap/artifactIdversion
5.
3/version/dependency!-- jQuery --dependencygroupIdorg.webjars/groupIdartifactIdjquery/artifactIdversion
3.
1/version/dependencyHTML 中使用两种方式带版本推荐最高效无需额外依赖linkrelstylesheethref/webjars/bootstrap/
5.
3/dist/css/bootstrap.min.cssscriptsrc/webjars/jquery/
3.
1/jquery.min.js/script不带版本版本无关需要额外依赖!-- 额外加这个依赖 --dependencygroupIdorg.webjars/groupIdartifactIdwebjars-locator-lite/artifactId/dependency!-- 自动解析到最新版本 --linkrelstylesheethref/webjars/bootstrap/dist/css/bootstrap.min.css自定义 WebJars 路径很少需要但可以registry.addResourceHandler(/assets/lib/**).addResourceLocations(classpath:/META-INF/resources/webjars/);
4.