SpringBoot静态资源配置原理

SpringBoot静态资源配置原理

1. 配置类

首先,打开WebMvcAutoConfiguration类,其中的一个静态类WebMvcAutoConfigurationAdapter下有一个addResourceHandlers方法

@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
		}

2. webjars方式请求静态资源(了解即可,用得很少)

该段代码中第一个 if 条件判断是针对URL中输入webjars来访问静态资源的

			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}

其中 " webjars/** " 要求访问静态资源时须在URL中加入 webjars/** 才能访问,如下图
在这里插入图片描述
当我们以这种方式访问静态资源,SpringBoot就会从classpath:/META-INF/resources/webjars/下找寻静态资源并返回给用户

以这种方式访问静态资源,前提条件是得向pom.xml中以依赖的形式导入静态资源,例如,导入jquery


			<dependency><!--Jquery组件(前端)-->
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>3.3.1</version>
            </dependency>

3. 通过设置静态资源路径模式访问静态资源(重点)

String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
	customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
			.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
			.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}

分析上面这段代码

  1. 首先String staticPathPattern = this.mvcProperties.getStaticPathPattern();是获得默认的静态资源的路径模式
    在这里插入图片描述
    如上图所示,从源码中可以看到默认是" / ** ",即直接从地址栏中输入静态资源名称即可访问,例如
    http://localhost:8080/jquery.min.js
    即可访问到对应的静态资源
    此处的路径模式也可以修改(下文会说)

  2. 紧接着下面 if 语句中的代码 registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()) 就是配置静态资源映射的路径。
    其中staticPathPattern是上文提到的默认的获得静态资源的路径模式。
    addResourceLocations方法则是添加一个或多个资源位置以从其中提供静态内容,点开this.resourceProperties.getStaticLocations()可发现所添加的资源位置即为classpath下的四个目录(下图上方红框所示

    即我们在地址栏输入静态资源的文件名称,springboot会自动地去这些目录中寻找并返回给我们

    在这里插入图片描述

  3. 当然,我们也可以设置静态资源的路径模式,使其不为 /**
    只需在application.yml中自定义即可(这里我设为static)

spring:
  mvc:
    static-path-pattern: /static/**

这样设置后,如果再访问静态资源的话,路径前必须加上static,否则访问不到,例如

http://localhost:8080/static/jquery.min.js
注意:此处定义了新的路径模式,那么addResourceHandlers方法就会执行第一个if语句return返回,就不会再往下执行默认的两个访问方式了
if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
		return;
}

希望对您学习有帮助,谢谢

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值