SpringBoot处理静态资源

本文探讨了SpringBoot中如何处理静态资源,如.css和.js文件。通过源码分析,揭示了SpringBoot的MVC自动配置,包括WebMvcAutoConfiguration中的静态资源处理方法。文章介绍了两种加载资源的方式:一是利用webjars,通过maven引入前端资源;二是自定义资源路径映射,但此方式会覆盖默认配置,除非涉及META-INF/resources路径。
摘要由CSDN通过智能技术生成

1、思考:SpringBoot是没有webapp目录的那么我们的资源( *.css , *.js …)应该放在哪里?

2、通过源码探析

1、研究SpringBoot关于MVC的自动配置

1、所有关于MVC的相关配置都在WebMvcAutoConfiguration(视图解析,静态资源过滤)
2、addResourceHandlers静态资源处理方法

public void addResourceHandlers(ResourceHandlerRegistry registry) {
	//1、禁止默认规则的一个配置,如果手动添加了资源映射路径配置,则自动配置就会失效
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
    	//缓存控制
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        //判断是否存在一个映射路径/webjars/**
        //addResourceHandler处理逻辑/webjars/a.js
        //addResourceLocations 处理资源的地址classpath:/META-INF/resources/webjars/
        if (!registry.hasMappingForPattern("/webjars/**")) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

		//获取静态资源路径
		//此处点击源码获取的就是一个字符串 /**
        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        //如果访问映射的路径是staticPathPattern = "/**";
        if (!registry.hasMappingForPattern(staticPathPattern)) {
        //this.resourceProperties.getStaticLocations()获取四个常量路径
       // private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { 	
        	//"classpath:/META-INF/resources/",
			//"classpath:/resources/", 
			//"classpath:/static/", 
			//"classpath:/public/" };
        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}

2、基于源码分析引出加载资源方式

1、第一种:webjars

1、什么是webjars:使用maven,通过jar包的方式,引入前端静态资源
2、导入maven

<dependency>
	 <groupId>org.webjars</groupId> 
	 <artifactId>jquery</artifactId>
 	 <version>3.4.1</version> 
</dependency>

2、测试访问
在这里插入图片描述

2、第二种:

1、4种路径,建项目是默认只有static

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { 	
     "classpath:/META-INF/resources/",// 在 starter 中使用! SWAGGER-UI
	"classpath:/resources/", // 文件资源
	"classpath:/static/", // 静态资源
	"classpath:/public/" // 公共的,图标......
	}

2、四种路径加载加载先后顺序:
META-INF/resources>resources>public>static
在这里插入图片描述

3、第二种:自己配置了资源路经的映射,默认的配置就会失效

注:看源码配置了自己的映射路径默认的应该都失效,但是META-INF下的为啥没失效呢??待解答
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值