SpringBoot处理静态资源

静态资源

WebMvcAutoConfiguration.class配置类中的addResourceHandlers方法的源码

WebMvcAutoConfiguration.java源码中找到addResourceHandlers方法

   	public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
   		//this.resourceProperties.isAddMappings()如果对resourceProperties进行配置,那么就会禁用默认资源处理
   		//resourceProperties.isAddMappings() 默认为true
			if (!this.resourceProperties.isAddMappings()) {
    
    
				logger.debug("Default resource handling disabled");
				return;
			}
			//获取缓存配置的资源处理程序提供的资源缓存周期。
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			//getCache().getCachecontrol()缓存控制HTTP头配置
			//以下一段是将classpath:/META-INF/resources/webjars/映射为/webjars/**
			//只要存在/META-INF/resources/webjars/都可以通过/webjars/**进行访问
			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));
			}
			//默认值
			//staticPathPattern = "/**"
			//String[] staticLocations ={ "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" }
			//以下这段代码可以通过"/**"访问staticLocations包含 的路径下去找资源
			//资源访问存在优先级
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
    
    
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
		}
123456789101112131415161718192021222324252627282930

springboot中使用webjars加载静态资源

首先需要去webjars(https://www.webjars.org/)网站,然后搜maven依赖,把maven依赖加入到springboot项目的pom.xml文件中,
网站如下图
在这里插入图片描述
然后成功把坐标加入到pom.xml文件中的依赖后会在项目中出现以下的这个包如图

然后去WebMvcAutoConfiguration.class配置类中addResourceHandlers方法,如下图

在这里插入图片描述

在这里插入图片描述

这样我们访问下面图片中的路径的时候,

在这里插入图片描述

比如想访问jquery.js静态资源,那么可以直接写接口localhost:8080/webjars/jquery/3.4.1/jquery.js,需要注意的是我们源码中是把/webjars路径代替classpath:/resources/webjars路径,并没有连同后面的/jquery/3.4.1路径,因此后面的写接口的时候webjars/后面一定要加上/jquery/3.4.1即localhost:8080/webjars/jquery/3.4.1/jquery.js。

用webjars处理静态资源的这种方式不常用,常用的是通过静态位置staticLocations来处理静态资源,也即是把静态资源直接放进特定名称的目录里面,然后就可以直接访问了。

springboot中使用staticLocations加载静态资源

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

从上面的5张图片可以得出一个结论,路径/可以代替classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,

classpath:/public这四个静态路径。

这四个静态路径的优先级:/META-INF/resources/>/resources>/static(默认)>/public

在这里插入图片描述

如上图是resources根目录下的四个静态目录,这四个静态目录里面都有一个名字为hello.js的静态文件,然后通过浏览器访问接口

localhost:8080/hello.js访问的是优先级最高的静态目录里面的文件,也即是META-INF/resources/hello.js

使用静态资源注意事项

使用静态资源的目录的时候,假设把所有的静态文件都放入了classpath:/static静态文件夹下,加入有一个静态文件是hello.js,然后正常情况下,

在这里插入图片描述

即没有对resourceProperties进行配置的时候,可以在浏览器中使用localhost:8080/hello.js接口可以生效,但是如果在配置文件中对resourceProperties进行了配置,如下图

在这里插入图片描述

那么再次访问静态资源时就要用localhost:8080/static/hello.js接口了,如果仍是用localhost:8080/hello.js接口会出错。

WebMvcAutoConfiguration都可以在配置文件中配置什么信息怎么看

找到WebMvcAutoConfiguration.class配置类中@EnableConfigurationProperties({WebMvcProperties.class})注解,然后点击WebMvcProperties.class类,结合这个类里面的@ConfigurationProperties(prefix=“spring.mvc”)和这个类中的属性的名字可以知道

配置文件中能配置的与Web模块相关的配置信息就是spring.mvc…

其中WebMvcProperties.class类中有一个属性就是spring.mvc.static-path-pattern如果此属性在配置文件中配置了,如上图中的情况,那么访问静态资源的接口就不再是localhost:8080/了。

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45950109/article/details/110773604