SpringBoot靜態(tài)資源與首頁配置實現(xiàn)原理深入分析
一、靜態(tài)資源導入
關鍵源碼可以看WebMvcAutoConfiguration這個類下面的addResourceHandlers方法
在這個方法中,我們有幾個重點需要了解一下
1、webjars
可以理解為以maven的形式引入web的相關jar包
請求路徑為/webjars/**
的,都會去classpath:/META-INF/resources/webjars/
下尋找相關的靜態(tài)資源
2、靜態(tài)資源映射規(guī)則
如果在項目中要使用我們自己導入的靜態(tài)資源,它的映射規(guī)則是怎么樣的呢,我們分析源碼可以得出
以下四個路徑的中存放的靜態(tài)資源可以被識別,優(yōu)先
resource(注意,此處是resource下面的resource文件夾)>static (默認)>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,當前項目 + static-path-pattern + 靜態(tài)資源名 = 靜態(tài)資源文件夾下找
spring:
web:
resources:
static-locations: classpath:/static/
mvc:
static-path-pattern: /static/**
隨后我們訪問一下靜態(tài)資源,發(fā)現(xiàn)只有放在static下面可以被訪問到
二、首頁配置和圖標
1、首頁配置
springboot它會去找靜態(tài)資源文件夾下的index.html
(注意不能配置spring.mvc.static-path-pattern)或者是controller處理/index
轉發(fā)的頁面
下面是WebMvcAutoConfiguration這個類中關于首頁的相關方法
@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、圖標
官網是說在靜態(tài)資源路徑下放置一個favicon.ico,spring boot就會自動識別
如圖
圖標加載成功 可能會因為緩存加載不出來 清除緩存多試幾次就行了
到此這篇關于SpringBoot靜態(tài)資源與首頁配置實現(xiàn)原理深入分析的文章就介紹到這了,更多相關SpringBoot靜態(tài)資源內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java設置token有效期的5個應用場景(雙token實現(xiàn))
Token最常見的應用場景之一就是身份驗證,本文主要介紹了Java設置token有效期的5個應用場景(雙token實現(xiàn)),具有一定的參考價值,感興趣的可以來了解一下2024-04-04Springboot Mybatis-Plus數(shù)據庫單元測試實戰(zhàn)(三種方式)
這篇文章主要介紹了Springboot Mybatis-Plus數(shù)據庫單元測試實戰(zhàn)(三種方式),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Java 實現(xiàn)Redis存儲復雜json格式數(shù)據并返回給前端
這篇文章主要介紹了Java 實現(xiàn)Redis存儲復雜json格式數(shù)據并返回給前端操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07springIOC的使用流程及spring中使用類型轉換器的方式
Spring IOC是Spring框架的核心原理之一,它是一種軟件設計模式,用于管理應用程序中的對象依賴關系,這篇文章主要介紹了springIOC的使用流程以及spring中如何使用類型轉換器,需要的朋友可以參考下2023-06-06Spring?Boot指標監(jiān)控及日志管理示例詳解
Spring Boot Actuator可以幫助程序員監(jiān)控和管理SpringBoot應用,比如健康檢查、內存使用情況統(tǒng)計、線程使用情況統(tǒng)計等,這篇文章主要介紹了Spring?Boot指標監(jiān)控及日志管理,需要的朋友可以參考下2023-11-11