Spring Boot web开发 - 静态资源配置

318 阅读2分钟

参考的spring boot教程还是讲的传统web程序开发,如今这种传统架构已经被前后端分离架构替代。所以对本篇的笔记可以理解就好,毕竟后面不会再用,更多关于前后端分离框架的概念可以参考👇

Choose Between Traditional Web Apps and Single Page Apps (SPAs)

传统架构与前后端分离架构分析对比介绍

前后端分离架构概述

--------------------------------- 分割线 ---------------------------------------------------

用SpringBoot创建项目过程:

1)创建SpringBoot应用,选中我们需要的模块;

2)SpringBoot已经默认将这些场景配置好了,只需要在配置文件中制定少量配置就可以运行起来;

3)编写业务代码;

用好SpringBoot的关键:自动配置原理

这个场景SpringBoot帮我们配置了什么,能不能修改,能修改那些配置,能不能拓展。

xxxAutoConfiguration:帮我们给容器中自动配置组件。

xxxProperties:配置类来封装配置文件的内容。

WebMvcAutoConfiguration

所有的自动配置文件都记录到spring-autoconfigure-metadata.properties内,如想找到关于web的自动配置,可以在上述文件中搜索webmvcautoconfigure即可获取到路径,Command+B即可进入spring web的自动配置文件查看具体配置过程。

addResourceHandler方法添加资源映射

@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));
	}
}
  1. 所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源

    webjars是以jar包的方式引入web静态资源;webjars官网提供各种前段框架的依赖。

localhost:8080/webjars/source_name.js请求都会去类路径下/META-INF/resources/webjars/找。可以在localhost中试。如http://localhost:8081/webjars/jquery/3.5.1/jquery.js就可以访问到。

需要做的是引入所有需要的资源的依赖。

  1. /** 访问当前项目的任何资源(静态资源的文件夹)

    /**

    • Path pattern used for static resources. */ private String staticPathPattern = "/**";"classpath:/META-INT/resources/",

    "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/": 当前的根目录

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

把静态资源保存在以上任意文件夹中,都可以访问。

访问路径为:localhost:/8080/file === 去classpath:/resources/静态资源文件夹中找名为file的文件

  1. 欢迎页:静态资源文件夹下的所有index.html页面;被"/**"映射;

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

localhost:8080/ 默认会找index.html页面

  1. 所有的**/favicon.co 都是在静态资源文件夹下找。

自动配置静态资源文件夹的路径:

spring.resources.static-location=classpath:/hello/, classpath:/demo/

Reference:

webjars官网:www.webjars.org/