【SpringBoot3】实现自定义配置——以静态资源自定义配置为例(源码+代码示例)

1 配置类位置

  • 在左侧搜索autoconfigure可以找到·spring-boot-autoconfigure包,打开其下的META-INF -> spring -> AutoConfiguration.imports`
    在这里插入图片描述
  • 里面是SpringBoot启动会默认加载 142个配置类,但是其实并不是所有的配置类都会生效,会根据注解@ConditionOnXXX,按需生效
    在这里插入图片描述
  • ctrl点击进入其中的org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
    在这里插入图片描述
  • 其中的WebMvcAutoConfigurationAdapter实现了WebMvcConfigurerWebMvcConfigurer提供了配置SpringMVC底层的所有组件入口

在这里插入图片描述

alt+7 可以获得接口定义的所有的方法

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/**

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 06:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 06:50:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 06:50:02       82 阅读
  4. Python语言-面向对象

    2024-01-11 06:50:02       91 阅读

热门阅读

  1. 数据分析---SQL(3)

    2024-01-11 06:50:02       59 阅读
  2. Jtti:如何实现tomcat自动化部署

    2024-01-11 06:50:02       54 阅读
  3. 若依前后端分离版使用mybatis-plus实践教程

    2024-01-11 06:50:02       62 阅读
  4. Spring Boot “How-to“ 指南中文文档-上

    2024-01-11 06:50:02       45 阅读
  5. K8S--- kubectl auth

    2024-01-11 06:50:02       54 阅读
  6. 前端学习笔记 5:大事件

    2024-01-11 06:50:02       47 阅读
  7. K8S---kubectl options

    2024-01-11 06:50:02       55 阅读
  8. ELK 企业级日志分析系统

    2024-01-11 06:50:02       40 阅读
  9. ELK

    2024-01-11 06:50:02       43 阅读
  10. Docker基础

    2024-01-11 06:50:02       56 阅读