SpringBoot靜態(tài)資源及原理解析
一、使用 SpringBoot 的步驟
【1】創(chuàng)建SpringBoot
應用,選中自己需要的模塊。
【2】SpringBoot
已經(jīng)默認將這些場景配置好,只需要在配置文件中指定少量配置就可以運行起來。
【3】編寫業(yè)務邏輯代碼。
二、自動配置原理
我們要了解SpringBoot
幫我們配置了什么?能不能修改?能修改那些配置?能不能擴展等等。
【1】xxxAutoConfiguration
:幫我們給容器中自動配置組件。
【2】xxxProperties
:配置來封裝配置文件的內容。
三、SpringBoot 對靜態(tài)資源的映射規(guī)則
當創(chuàng)建一個jar
工程時,想引入css
等靜態(tài)資源時,需要遵守SpringBoot
的靜態(tài)資源映射關系,通過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
實際上是以jar
包的方式引入靜態(tài)資源,可以參考官方文檔
? 獲取Jquery
的jar
包依賴:
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-1</version> </dependency>
? 進入導入的Jquery
的jar
包中,查看目錄結構如下:所有的 /webjars/**
,都去classpath:/META‐INF/resources/webjars/
找資源。例如:localhost:8080/webjars/jquery/3.3.1/jquery.js
(在訪問的時候,只需要寫webjars
下面資源的名稱即可)
【2】同時可以在ResourceProperties
設置與靜態(tài)資源有關的參數(shù),例如緩存時間。
@ConfigurationProperties( prefix = "spring.resources", ignoreUnknownFields = false ) public class ResourceProperties implements ResourceLoaderAware, InitializingBean {
【3】除了/webjars/**
,我們看下緊接著的第二個方法獲取staticPathPattern
路徑:最終方法指向 =/**
訪問當前項目的任何資源(靜態(tài)資源的文件夾)。
this.staticPathPattern = "/**";
如果沒有進行處理,就會從如下路徑中進行獲?。?/p>
classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" "/":當前項目根路徑 ,以上就是靜態(tài)資源的文件處理。
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};//當前項目根路徑 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()); } //進入如上的resourceProperties.getWelcomePage()方法,會獲取到當前項目路徑下的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; } //進入如上的this.mvcProperties.getStaticPathPattern()方法,獲取映射的路徑 this.staticPathPattern = "/**";
【5】所有的**/favicon.ico
都是從靜態(tài)文件中獲取一個favicon.ico
文件。圖標:
@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); //默認獲取圖標的位置和名稱 mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler())); return mapping; } }
【6】也可以在application.properties
全局配置文件中自定義靜態(tài)文件:在配置文件中設置如下,那么默認的就不在生效。
# 配置文件是一個數(shù)組,可以用逗號進行分隔 spring.resources.static-locations=classpath:/hello/,calsspath:/xxx/
到此這篇關于SpringBoot——靜態(tài)資源及原理的文章就介紹到這了,更多相關SpringBoot靜態(tài)資源內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別
這篇文章主要介紹了InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別的相關資料,需要的朋友可以參考下2015-12-12Java項目實戰(zhàn)之在線考試系統(tǒng)的實現(xiàn)(系統(tǒng)介紹)
這篇文章主要介紹了Java項目實戰(zhàn)之在線考試系統(tǒng)的實現(xiàn)(系統(tǒng)介紹),本文通過實例代碼,截圖的形式給大家展示系統(tǒng)技術架構,需要的朋友可以參考下2020-02-02Spring @Value如何通過${}、#{}注入不同類型的值
這篇文章主要介紹了Spring @Value如何通過${}、#{}注入不同類型的值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05