SpringBoot靜態(tài)資源與首頁(yè)配置實(shí)現(xiàn)原理深入分析
一、靜態(tài)資源導(dǎo)入
關(guān)鍵源碼可以看WebMvcAutoConfiguration這個(gè)類下面的addResourceHandlers方法
在這個(gè)方法中,我們有幾個(gè)重點(diǎn)需要了解一下
1、webjars
可以理解為以maven的形式引入web的相關(guān)jar包
請(qǐng)求路徑為/webjars/**
的,都會(huì)去classpath:/META-INF/resources/webjars/
下尋找相關(guān)的靜態(tài)資源
2、靜態(tài)資源映射規(guī)則
如果在項(xiàng)目中要使用我們自己導(dǎo)入的靜態(tài)資源,它的映射規(guī)則是怎么樣的呢,我們分析源碼可以得出
以下四個(gè)路徑的中存放的靜態(tài)資源可以被識(shí)別,優(yōu)先
resource(注意,此處是resource下面的resource文件夾)>static (默認(rèn))>public
"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
3、自定義靜態(tài)資源路徑
我們可以使用spring.web.resources.static-locations
在yaml文件中自定義靜態(tài)資源文件的路徑,例如我們限制靜態(tài)文件都必須放在static目錄下
也可以使用spring.mvc.static-path-pattern,當(dāng)前項(xiàng)目 + static-path-pattern + 靜態(tài)資源名 = 靜態(tài)資源文件夾下找
spring:
web:
resources:
static-locations: classpath:/static/
mvc:
static-path-pattern: /static/**
隨后我們?cè)L問(wèn)一下靜態(tài)資源,發(fā)現(xiàn)只有放在static下面可以被訪問(wèn)到
二、首頁(yè)配置和圖標(biāo)
1、首頁(yè)配置
springboot它會(huì)去找靜態(tài)資源文件夾下的index.html
(注意不能配置spring.mvc.static-path-pattern)或者是controller處理/index
轉(zhuǎn)發(fā)的頁(yè)面
下面是WebMvcAutoConfiguration這個(gè)類中關(guān)于首頁(yè)的相關(guān)方法
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations()); return welcomePageHandlerMapping; } private Resource getWelcomePage() { String[] var1 = this.resourceProperties.getStaticLocations(); int var2 = var1.length; for(int var3 = 0; var3 < var2; ++var3) { String location = var1[var3]; Resource indexHtml = this.getIndexHtml(location); if (indexHtml != null) { return indexHtml; } } ServletContext servletContext = this.getServletContext(); if (servletContext != null) { return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/"))); } else { return null; } } private Resource getIndexHtml(String location) { return this.getIndexHtml(this.resourceLoader.getResource(location)); } private Resource getIndexHtml(Resource location) { try { Resource resource = location.createRelative("index.html"); if (resource.exists() && resource.getURL() != null) { return resource; } } catch (Exception var3) { } return null; }
2、圖標(biāo)
官網(wǎng)是說(shuō)在靜態(tài)資源路徑下放置一個(gè)favicon.ico,spring boot就會(huì)自動(dòng)識(shí)別
如圖
圖標(biāo)加載成功 可能會(huì)因?yàn)榫彺婕虞d不出來(lái) 清除緩存多試幾次就行了
到此這篇關(guān)于SpringBoot靜態(tài)資源與首頁(yè)配置實(shí)現(xiàn)原理深入分析的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)置token有效期的5個(gè)應(yīng)用場(chǎng)景(雙token實(shí)現(xiàn))
Token最常見的應(yīng)用場(chǎng)景之一就是身份驗(yàn)證,本文主要介紹了Java設(shè)置token有效期的5個(gè)應(yīng)用場(chǎng)景(雙token實(shí)現(xiàn)),具有一定的參考價(jià)值,感興趣的可以來(lái)了解一下2024-04-04Springboot Mybatis-Plus數(shù)據(jù)庫(kù)單元測(cè)試實(shí)戰(zhàn)(三種方式)
這篇文章主要介紹了Springboot Mybatis-Plus數(shù)據(jù)庫(kù)單元測(cè)試實(shí)戰(zhàn)(三種方式),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java 實(shí)現(xiàn)Redis存儲(chǔ)復(fù)雜json格式數(shù)據(jù)并返回給前端
這篇文章主要介紹了Java 實(shí)現(xiàn)Redis存儲(chǔ)復(fù)雜json格式數(shù)據(jù)并返回給前端操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07springIOC的使用流程及spring中使用類型轉(zhuǎn)換器的方式
Spring IOC是Spring框架的核心原理之一,它是一種軟件設(shè)計(jì)模式,用于管理應(yīng)用程序中的對(duì)象依賴關(guān)系,這篇文章主要介紹了springIOC的使用流程以及spring中如何使用類型轉(zhuǎn)換器,需要的朋友可以參考下2023-06-06java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Spring?Boot指標(biāo)監(jiān)控及日志管理示例詳解
Spring Boot Actuator可以幫助程序員監(jiān)控和管理SpringBoot應(yīng)用,比如健康檢查、內(nèi)存使用情況統(tǒng)計(jì)、線程使用情況統(tǒng)計(jì)等,這篇文章主要介紹了Spring?Boot指標(biāo)監(jiān)控及日志管理,需要的朋友可以參考下2023-11-11