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

6 篇文章 2 订阅
4 篇文章 0 订阅

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/**
  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值