浅析SpringBoot静态资源访问

Springboot中SpringMVC的web配置都在WebMvcAutoConfiguration这个配置类中。WebMvcAutoConfigurationAdapter类中有一个addResourceHandlers方法用来添加资源。

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //如果我们自定义了资源导入的位置,则默认的配置都将会失效
    //自定义spring.resources.static-locations=classpath:/coding/
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    //webjars配置
   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));
   }
}

通过源码可知,所有的/webjars/**,都会去classpath:/META-INF/resources/webjars/下找对应资源

webjars是啥?

webjars就是以jar包的方式引入静态资源。

网站:https://www.webjars.org

如果我们要使用jQuery直接将依赖导入即可

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

目录结构:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fas0bCcI-1614758611356)(G:\Typora\SpringBoot学习笔记.asserts\image-20201031211756891.png)]
此时就可以启动项目直接使用localhost:8080/webjars/jquery/3.5.1/jquery.js访问jquery.js

那么我们传统的静态资源在哪里呢?不慌,接着看上面的源码:
点进staticPathPattern看看静态资源是通过什么请求访问的:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6A7tWl61-1614758611359)(G:\Typora\SpringBoot学习笔记.asserts\image-20201031212717784.png)]
/** 请求就是访问静态资源的,继续看Springboot会去哪里加载静态资源,点进staticLocations看看:
在这里插入图片描述
通过源码可知,所有的 /**,会去访问如图的四个路径。

加载静态资源的优先级(从大到小): Defaults to classpath:[/META-INF/resources/, /resources/, /static/, /public/].

那么首页访问是什么地址呢?

继续看WebMvcAutoConfiguration类中EnableWebMvcConfigurationr类中的源码

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
														   FormattingConversionServicemvcConversionService, 
                                                           ResourceUrlProvider mvcResourceUrlProvider) {
	WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
				new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
				this.mvcProperties.getStaticPathPattern());	
	welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService,mvcResourceUrlProvider));
	welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
	return welcomePageHandlerMapping;
}

private Optional<Resource> getWelcomePage() {
    	//参照前面的分析,这里的locations就是静态资源访问的四种路径
		String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
		return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}

private Resource getIndexHtml(String location) {
    //欢迎页就是一个location下的的 index.html 而已
	return this.resourceLoader.getResource(location + "index.html");
}

所以说首页就是静态资源访问路径下的index.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值