解释 src/main/resources 下 static、templates、META-INF 的默认扫描规则

解读

国内面试官问这个,不是考“三个文件夹叫啥”,而是看候选人是否真正理解 Spring Boot 的“资源加载链”与“类加载器委托模型”。答不出“底层是 StandardServletEnvironment 与 ResourceLoader 的协同”,就会被追问“那我把 static 挪到 jar 外怎么改?”“templates 里的文件为何不走 Tomcat DefaultServlet?”——现场一紧张就翻车。因此,必须把“谁扫、怎么扫、扫完给谁用”说透,并给出国内工程中最容易踩坑的三处默认值。

知识点

  1. ResourceProperties(spring.resources.*)里对 static-locations、template-loader-path 的默认映射
  2. DispatcherServlet 静态资源处理链:HandlerMapping → ResourceHttpRequestHandler → ServletContextResource/ClasspathResource
  3. TemplateAvailabilityProvider 与 ViewResolver 的“前缀+后缀”拼接规则
  4. spring.factories 与 spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 的 SPI 加载顺序(Spring Boot 2.7+ 双轨兼容)
  5. Maven/Gradle 打包后,“classpath 根”在 jar 内部 BOOT-INF/classes/,而 dev-tools 重启时额外加入的重启类加载器如何影响路径优先级
  6. 国内容器云(阿里云 SAE、腾讯云 TKE)把 jar 解压到 /app 后,ServletContext.getRealPath() 为 null,因此必须 100% 走 classpath 读取,不能依赖文件系统 API

答案

  1. static 目录
    a. 默认被 ResourceHttpRequestHandler 注册到 /**,位置列表由 spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 决定,顺序即优先级。
    b. 打包后存在于 BOOT-INF/classes/static/,运行时由 LaunchedURLClassLoader 当作 classpath 根下资源;外部化部署时,可在 jar 同级建 static/ 目录,无需改代码即可覆盖 jar 内文件,这是国内灰度发布最常用的“静态资源热替换”技巧。
    c. 缓存头默认 cache-period=0,生产必须显式配置 spring.resources.cache.period=86400s,否则压测会被扣“静态资源无缓存”分。

  2. templates 目录
    a. 由 TemplateAvailabilityProvider 探测,仅当类路径下存在 thymeleaf、freemarker、groovy-templates 其中任一引擎的 TemplateEngine 时,才会把 classpath:/templates/ 注册成 TemplateResolver 的前缀。
    b. 文件不会被 ResourceHttpRequestHandler 公开,直接访问 /templates/hello.html 返回 404;必须通过 @Controller 返回视图名,由 ViewResolver 拼前缀+后缀后加载。
    c. 国内银行项目常把 templates 放到 jar 外部以通过 行内安全扫描,此时在 application-prod.yml 里加 spring.thymeleaf.prefix=file:/opt/web/templates/ 即可,无需重启。

  3. META-INF 目录
    a. spring.factories 与 spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 由 SpringFactoriesLoader 在 Bootstrap 阶段读取,时机早于 @SpringBootApplication 主类,因此修改内容后必须重新打包,仅重启应用不生效。
    b. META-INF/resources/ 被视为 webjars 入口,优先级高于 static,因此国内项目若把 Vue 打包后的 dist 直接塞到这里,可省略 nginx,但注意路径冲突会导致 404。
    c. 国内私有仓库(如 Nexus 私服)上传 starter 时,必须保留 META-INF/spring.factories,否则下游应用无法自动装配,这是新人最容易被运维“退包”的原因。

一句话总结:
static 给浏览器,templates 给视图引擎,META-INF 给 Spring Boot 自己;三者扫描顺序由 ResourceProperties、Servlet 容器、SpringFactoriesLoader 三条独立管道完成,互不干扰,但都可被 spring. 前缀配置外部化。*

拓展思考

  1. 如果公司要求“前端包走 CDN,但本地 dev 要热加载”,如何只改 profile就让 static 目录指向 **http://localhost:3000/dist/**?
    提示:利用 spring.resources.static-locations 支持 URL 前缀,dev 环境加 **http://localhost:3000/dist/**,prod 环境留空即可。

  2. 当项目同时存在 thymeleaf 与 freemarker,templates 下同名 hello.ftl 与 hello.html 谁会赢?
    提示:看 TemplateAvailabilityProviders 的 Order 值,ThymeleafAutoConfiguration 默认 @Order(1),因此 hello.html 优先;可通过实现 TemplateAvailabilityProvider 并提高 Order 来让自由标记引擎插队。

  3. 国内信创环境要求禁用 spring.factories,只允许 AutoConfiguration.imports,如何一行命令快速校验 jar 是否合规?
    提示:

    jar tf your-app.jar | grep -E 'spring\.factories|AutoConfiguration\.imports' | awk -F/ '{print $NF}' | sort | uniq -c
    

    若 spring.factories 存在且 AutoConfiguration.imports 不存在,则打包脚本需升级 spring-boot-maven-plugin 到 3.x 并打开 -parameters 选项。