这里写目录标题
- 1 配置类位置
- 2 静态资源配置方式
- 3 整体配置示例
- 3.1 创建配置类
- 3.2 实现配置方法
- 3.3 指定配置文件属性
1 配置类位置
- 在左侧搜索
autoconfigure可以找到·spring-boot-autoconfigure包,打开其下的META-INF -> spring -> AutoConfiguration.imports`

- 里面是SpringBoot启动会默认加载 142个配置类,但是其实并不是所有的配置类都会生效,会根据注解@ConditionOnXXX,按需生效

- 按
ctrl点击进入其中的org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration。

- 其中的
WebMvcAutoConfigurationAdapter实现了WebMvcConfigurer,WebMvcConfigurer提供了配置SpringMVC底层的所有组件入口

2 静态资源配置方式
- 在
application.properties或者application.yaml中增加配置。因为WebMvcAutoConfigurationAdapter上的注解@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })因此选择在这两个文件中找配置的属性

- 其中,
WebMvcProperties定义的属性以spring.mvc为开头

WebProperties定义的属性以spring.web为开头

3 整体配置示例
3.1 创建配置类
创建一个配置类,通常使用 @Configuration 注解标记,并在该类中定义配置方法。
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 CustomWebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 配置自定义的静态资源路径,例如将所有访问路径以 "/my-resources/" 开头的请求映射到 "classpath:/my-static-resources/"
registry.addResourceHandler("/my-resources/**")
.addResourceLocations("classpath:/my-static-resources/");
}
}
在这个例子中,CustomWebMvcConfig 类实现了 WebMvcConfigurer 接口,覆盖了其中的 addResourceHandlers 方法,用于配置自定义的静态资源路径。
3.2 实现配置方法
在配置类中定义用于自定义静态资源配置的方法,使用 addResourceHandlers 方法。如果你希望将自定义的配置放在application.properties 中,可以使用 @Value 注解注入属性值,例如:
import org.springframework.beans.factory.annotation.Value;
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 CustomWebMvcConfig implements WebMvcConfigurer {
@Value("${custom.static.resource.path}")
private String customStaticResourcePath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/my-resources/**")
.addResourceLocations(customStaticResourcePath);
}
}
3.3 指定配置文件属性
如果需要,你可以在application.properties(或 application.yml)中定义一些属性,以便在配置类中引用(只是改值,也可以不写自己的配置类)。
- spring.mvc: 静态资源访问前缀路径
- spring.web:
- 静态资源目录
- 静态资源缓存策略
application.properties如下设置:
#1、spring.web:
# 1.配置国际化的区域信息
# 2.静态资源策略(开启、处理链、缓存)
#开启静态资源映射规则
spring.web.resources.add-mappings=true
#设置缓存
spring.web.resources.cache.period=3600
##缓存详细合并项控制,覆盖period配置:
## 浏览器第一次请求服务器,服务器告诉浏览器此资源缓存7200秒,7200秒以内的所有此资源访问不用发给服务器请求,7200秒以后发请求给服务器
spring.web.resources.cache.cachecontrol.max-age=7200
## 共享缓存
spring.web.resources.cache.cachecontrol.cache-public=true
#使用资源 last-modified 时间,来对比服务器和浏览器的资源是否相同没有变化。相同返回 304
spring.web.resources.cache.use-last-modified=true
#自定义静态资源文件夹位置
spring.web.resources.static-locations=classpath:/a/,classpath:/b/,classpath:/static/
#2、 spring.mvc
## 2.1. 自定义webjars路径前缀
spring.mvc.webjars-path-pattern=/wj/**
## 2.2. 静态资源访问路径前缀
spring.mvc.static-path-pattern=/static/**











![P1067 [NOIP2009 普及组] 多项式输出————C++](https://img-blog.csdnimg.cn/direct/8986bdce9f9746ffa9b500205f0e89b0.png)







