目录
1. 源码位置
在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));
}
}
2. webjars配置
进入:https://www.webjars.org/,将maven的坐标粘贴到pom文件中
会看到在maven引入的包中包含了这个js文件
3. 在Springboot,我们可以使用以下方式访问静态资源
webjars目录的访问地址:
locatlhost:8080/wabjars/**
META-INF/resources/ ,public ,static , /** ,resources 的目录访问地址
localhost:8080/
4. 访问优先级
通过源码,我们发现staticLocations 存在的路径中都会映射为"/**",当其他存在相同文件时,则会按照以下的顺序进行进行访问
1. META-INF/resources/
2. resources
3. static(默认)
4. public
5. index首页源码分析
@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");
}
6. src/main/resources目录和src/main/webapp目录的区别
6.1 resources目录下,创建下面四个目录,这些目录中的静态资源会会被浏览器直接访问到。修改里面的文件内容,刷新浏览器,不会同步更新。
1. META-INF/resources/
2. resources
3. static(默认)
4. public
6.2 webapp目录下,WEB-INF中的资源文件不会被浏览器访问到。webapp目录下其它位置的文件会被浏览器访问,并且修改文件内容后,刷新浏览器,同步更新。
6.3 如果resources目录下和webapp目录下存在同名的静态资源,优先访问resources目录下的静态资源;同理,默认的首页会优先选择resources目录下的index.html。
6.4 maven插件在进行compile时,只会扫描到resources目录下的资源文件,想要扫描到webapp目录下的资源文件需要配置pom文件,添加对webapp目录的扫描。但是在进行package和install时,会同时扫描resources和webapp目录下的资源文件