自动配置原理
- xxxAutoConfiguration:给容器中自动配置组件
- xxxProperties:封装配置文件的配置类
映射静态资源
Web自动配置类:WebMvcAutoConfiguration
Web配置的封装类:MvcProperties 和 ResourceProperties
配置和资源有关的参数:
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
...
}
下面的代码是 WebMvcAutoConfiguration 类的静态资源加载方法的源码:
// 静态资源配置类
@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 网站:https://www.webjars.org/
- WebJars 是将客户端(浏览器)网络资源库(例如 jQuery 或者 Bootstrap)打包成 JAR 文件,然后通过 maven 依赖导入,通过 localhost:8080/webjars/jquery/3.5.1/jquery.js 访问
2、String staticPathPattern = this.mvcProperties.getStaticPathPattern(); 在 MvcProperties 配置类找到该参数:
/**
* ResourceProperties 资源路径配置类中的一个参数
*/
private String staticPathPattern = "/**";
getResourceLocations(this.resourceProperties.getStaticLocations()) 静态资源的默认位置在 ResourceProperties 的源码中可以找到:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
自定义静态资源位置:通过 ResourceProperties 配置类中的 staticLocations 属性,因此,一旦自定义了资源位置,默认配置就会失效
spring:
resources:
static-locations: /static/
3、欢迎页:静态资源文件夹的所有 index.html 页面,同时被 /** 映射
/* WebMvcAutoConfiguration 的静态内部类 EnableWebMvcConfiguration 中的方法 */
@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;
}
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
// 拼凑出欢迎页的资源路径
return this.resourceLoader.getResource(location + "index.html");
}
/* WelcomePageHandlerMapping */
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
// 欢迎页的映射,/**会被重定向
if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage.get());
setRootViewName("forward:index.html");
}
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
setRootViewName("index");
}
}
4、图标资源:在静态资源文件下找 /favicon.ico