Spring Boot中如何配置静态资源使用协商缓存策略?

2026-05-23 22:591阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计208个文字,预计阅读时间需要1分钟。

Spring Boot中如何配置静态资源使用协商缓存策略?

版本+父项目+groupId+org.springframework.boot+groupId+artifactId+spring-boot-starter-parent+artifactId+版本+2.7.5+版本+相对路径+!++查找父项目+从仓库+--+/parent+设置示例+协作缓存+包+com.example.demo.config

版本

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <relativePath/> <!-- lookup parent from repository --> </parent>

设置示例:协商缓存

package com.example.demo.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.http.CacheControl; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { // 设置静态资源映射 @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 上传文件 registry.addResourceHandler("/upload/**") .addResourceLocations("file:public/upload/") .setCacheControl(CacheControl.noCache()); // 静态资源 registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/") .setCacheControl(CacheControl.noCache()); } }

参考 Spring Boot中设置静态资源不缓存

Spring Boot中如何配置静态资源使用协商缓存策略?

本文共计208个文字,预计阅读时间需要1分钟。

Spring Boot中如何配置静态资源使用协商缓存策略?

版本+父项目+groupId+org.springframework.boot+groupId+artifactId+spring-boot-starter-parent+artifactId+版本+2.7.5+版本+相对路径+!++查找父项目+从仓库+--+/parent+设置示例+协作缓存+包+com.example.demo.config

版本

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <relativePath/> <!-- lookup parent from repository --> </parent>

设置示例:协商缓存

package com.example.demo.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.http.CacheControl; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { // 设置静态资源映射 @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 上传文件 registry.addResourceHandler("/upload/**") .addResourceLocations("file:public/upload/") .setCacheControl(CacheControl.noCache()); // 静态资源 registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/") .setCacheControl(CacheControl.noCache()); } }

参考 Spring Boot中设置静态资源不缓存

Spring Boot中如何配置静态资源使用协商缓存策略?