SpringBoot靜態(tài)資源及原理解析
一、使用 SpringBoot 的步驟
【1】創(chuàng)建SpringBoot應(yīng)用,選中自己需要的模塊。
【2】SpringBoot已經(jīng)默認(rèn)將這些場(chǎng)景配置好,只需要在配置文件中指定少量配置就可以運(yùn)行起來。
【3】編寫業(yè)務(wù)邏輯代碼。
二、自動(dòng)配置原理
我們要了解SpringBoot幫我們配置了什么?能不能修改?能修改那些配置?能不能擴(kuò)展等等。
【1】xxxAutoConfiguration:幫我們給容器中自動(dòng)配置組件。
【2】xxxProperties:配置來封裝配置文件的內(nèi)容。
三、SpringBoot 對(duì)靜態(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?Date(日期)對(duì)象進(jìn)行格式化的思路詳解
Date類是經(jīng)常會(huì)使用到的一個(gè)用來處理日期、時(shí)間的一個(gè)類。Date類是在java.util包下的Date類,這篇文章主要介紹了Java?Date(日期)對(duì)象如何進(jìn)行格式化呢,需要的朋友可以參考下2022-09-09
Springboot啟動(dòng)擴(kuò)展點(diǎn)超詳細(xì)教程小結(jié)
這篇文章主要介紹了Springboot啟動(dòng)擴(kuò)展點(diǎn)超詳細(xì)教程小結(jié),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別
這篇文章主要介紹了InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別的相關(guān)資料,需要的朋友可以參考下2015-12-12
Java項(xiàng)目實(shí)戰(zhàn)之在線考試系統(tǒng)的實(shí)現(xiàn)(系統(tǒng)介紹)
這篇文章主要介紹了Java項(xiàng)目實(shí)戰(zhàn)之在線考試系統(tǒng)的實(shí)現(xiàn)(系統(tǒng)介紹),本文通過實(shí)例代碼,截圖的形式給大家展示系統(tǒng)技術(shù)架構(gòu),需要的朋友可以參考下2020-02-02
Spring @Value如何通過${}、#{}注入不同類型的值
這篇文章主要介紹了Spring @Value如何通過${}、#{}注入不同類型的值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
java精度計(jì)算代碼 java指定精確小數(shù)位
這篇文章主要為大家詳細(xì)介紹了java精度計(jì)算代碼,java指定精確小數(shù)位,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

