SpringBoot靜態(tài)資源及原理解析
一、使用 SpringBoot 的步驟
【1】創(chuàng)建SpringBoot應(yīng)用,選中自己需要的模塊。
【2】SpringBoot已經(jīng)默認(rèn)將這些場景配置好,只需要在配置文件中指定少量配置就可以運(yùn)行起來。
【3】編寫業(yè)務(wù)邏輯代碼。
二、自動(dòng)配置原理
我們要了解SpringBoot幫我們配置了什么?能不能修改?能修改那些配置?能不能擴(kuò)展等等。
【1】xxxAutoConfiguration:幫我們給容器中自動(dòng)配置組件。
【2】xxxProperties:配置來封裝配置文件的內(nèi)容。
三、SpringBoot 對靜態(tài)資源的映射規(guī)則
當(dāng)創(chuàng)建一個(gè)jar工程時(shí),想引入css等靜態(tài)資源時(shí),需要遵守SpringBoot的靜態(tài)資源映射關(guān)系,通過WebMvcAutoConfiguration查看靜態(tài)配置資源的規(guī)則。
//添加資源映射addResourceHandlers
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if(!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
}
// 3中說明
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if(!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
}
}
}【1】如上配置的/webjars/**可知,所有的獲取都去classpath:/META-INF/resources/webjars下找資源。而webjar實(shí)際上是以jar包的方式引入靜態(tài)資源,可以參考官方文檔

? 獲取Jquery的jar包依賴:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-1</version>
</dependency>? 進(jìn)入導(dǎo)入的Jquery的jar包中,查看目錄結(jié)構(gòu)如下:所有的 /webjars/**,都去classpath:/META‐INF/resources/webjars/找資源。例如:localhost:8080/webjars/jquery/3.3.1/jquery.js(在訪問的時(shí)候,只需要寫webjars下面資源的名稱即可)

【2】同時(shí)可以在ResourceProperties設(shè)置與靜態(tài)資源有關(guān)的參數(shù),例如緩存時(shí)間。
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties implements ResourceLoaderAware, InitializingBean {【3】除了/webjars/**,我們看下緊接著的第二個(gè)方法獲取staticPathPattern路徑:最終方法指向 =/**訪問當(dāng)前項(xiàng)目的任何資源(靜態(tài)資源的文件夾)。
this.staticPathPattern = "/**";
如果沒有進(jìn)行處理,就會(huì)從如下路徑中進(jìn)行獲?。?/p>
classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" "/":當(dāng)前項(xiàng)目根路徑 ,以上就是靜態(tài)資源的文件處理。
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};//當(dāng)前項(xiàng)目根路徑
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private static final String[] RESOURCE_LOCATIONS;
static {
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length + SERVLET_RESOURCE_LOCATIONS.length];
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0, SERVLET_RESOURCE_LOCATIONS.length);
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, SERVLET_RESOURCE_LOCATIONS.length,
CLASSPATH_RESOURCE_LOCATIONS.length);
}【4】獲取歡迎頁,通過如下代碼可知:靜態(tài)資源文件夾下的所有index.html頁面,都被“/**”映射。(localhost:8080——就能夠訪問首頁)
// 獲取歡迎頁
@Bean
public WebMvcAutoConfiguration.WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {
return new WebMvcAutoConfiguration.WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
//進(jìn)入如上的resourceProperties.getWelcomePage()方法,會(huì)獲取到當(dāng)前項(xiàng)目路徑下的index.html文件。
private String[] getStaticWelcomePageLocations() {
String[] result = new String[this.staticLocations.length];
for(int i = 0; i < result.length; ++i) {
String location = this.staticLocations[i];
if(!location.endsWith("/")) {
location = location + "/";
}
result[i] = location + "index.html";
}
return result;
}
//進(jìn)入如上的this.mvcProperties.getStaticPathPattern()方法,獲取映射的路徑
this.staticPathPattern = "/**";【5】所有的**/favicon.ico都是從靜態(tài)文件中獲取一個(gè)favicon.ico文件。圖標(biāo):
@Configuration
@ConditionalOnProperty(
value = {"spring.mvc.favicon.enabled"},
matchIfMissing = true
)
public static class FaviconConfiguration {
private final ResourceProperties resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
//默認(rèn)獲取圖標(biāo)的位置和名稱
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
}【6】也可以在application.properties全局配置文件中自定義靜態(tài)文件:在配置文件中設(shè)置如下,那么默認(rèn)的就不在生效。
# 配置文件是一個(gè)數(shù)組,可以用逗號(hào)進(jìn)行分隔 spring.resources.static-locations=classpath:/hello/,calsspath:/xxx/
到此這篇關(guān)于SpringBoot——靜態(tài)資源及原理的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot JPA懶加載失效的解決方案(親測有效)
這篇文章主要介紹了SpringBoot JPA懶加載失效的解決方案(親測有效),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Intellj Idea中的maven工程Java文件顏色不對,未被識(shí)別的解決
這篇文章主要介紹了Intellj Idea中的maven工程Java文件顏色不對,未被識(shí)別的解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring?web開發(fā)教程之Request獲取3種方式
這篇文章主要給大家介紹了關(guān)于Spring?web開發(fā)教程之Request獲取3種方式的相關(guān)資料,request對象是從客戶端向服務(wù)器發(fā)出請求,包括用戶提交的信息以及客戶端的一些信息,需要的朋友可以參考下2023-11-11
Java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例
這篇文章主要介紹了java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
springboot下添加全局異常處理和自定義異常處理的過程解析
在spring項(xiàng)目中,優(yōu)雅處理異常,好處是可以將系統(tǒng)產(chǎn)生的全部異常統(tǒng)一捕獲處理,自定義的異常也由全局異常來捕獲,如果涉及到validator參數(shù)校驗(yàn)器使用全局異常捕獲也是較為方便,這篇文章主要介紹了springboot下添加全局異常處理和自定義異常處理,需要的朋友可以參考下2023-12-12
關(guān)于Controller 層返回值的公共包裝類的問題
本文給大家介紹Controller 層返回值的公共包裝類-避免每次都包裝一次返回-InitializingBean增強(qiáng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09

