跟我學springboot(十八)springboot-web-webjars和靜態資源對映規則
一.簡介
在使用springboot的web應用時,首先需要建立springboot應用,選擇我們自己需要的模組,springboot已經預設將這些場景配置好了,只需要在配置檔案指定少量的配置就可以執行起來。在使用之前我們需要了解自動配置的原理,可以參考跟我學springboot(十四)springboot自動配置原理核心的精髓如下:
1)、SpringBoot啟動會載入大量的自動配置類
2)、我們看我們需要的功能有沒有SpringBoot預設寫好的自動配置類;
3)、我們再來看這個自動配置類中到底配置了哪些元件;(只要我們要用的元件有,我們就不需要再來配置了)
4)、給容器中自動配置類新增元件的時候,會從properties類中獲取某些屬性。我們就可以在配置檔案中指定這些屬性的值;
xxxxAutoConfigurartion
xxxxProperties:封裝配置檔案中相關屬性
二.底層程式碼
我們以webMvc為例說明一下。
1、 WebMvcAutoConfiguration
首先是ResourceProperties,他可以配置一些和靜態資源有關的引數,比如快取,資源目錄等。
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
//可以設定和靜態資源有關的引數,快取時間等
addResourceHandlers方法主要是靜態資原始檔夾對映,所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找資源;webjars:以jar包的方式引入靜態資源;參考:http://www.webjars.org/, 比如我們引入jquery的包,在pom.xml配置:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version> 3.3.1-1</version>
</dependency>
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
//靜態資原始檔夾對映
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
我們訪問localhost:8080/webjars/jquery/3.3.1/jquery.js,就可以尋找到我們需要的靜態檔案。
“/**” 訪問當前專案的任何資源,都去(靜態資源的資料夾)找對映
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當前專案的根路徑
訪問:localhost:8080/abc === 去靜態資原始檔夾裡面找abc
歡迎頁; 靜態資原始檔夾下的所有index.html頁面;被"/**"對映;localhost:8080/ 找index頁面
//配置歡迎頁對映
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}
所有的 **/favicon.ico 都是在靜態資原始檔下找;
//配置喜歡的圖示
public static class FaviconConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(this.resolveFaviconLocations());
return requestHandler;
}
private List<Resource> resolveFaviconLocations() {
String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList(staticLocations.length + 1);
Stream var10000 = Arrays.stream(staticLocations);
ResourceLoader var10001 = this.resourceLoader;
this.resourceLoader.getClass();
var10000.map(var10001::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
}
}