SpringBoot中的靜態(tài)資源訪問的實(shí)現(xiàn)
一、說在前面的話
我們之間介紹過SpringBoot自動(dòng)配置的原理,基本上是如下:
xxxxAutoConfiguration:幫我們給容器中自動(dòng)配置組件; xxxxProperties:配置類來封裝配置文件的內(nèi)容;
二、靜態(tài)資源映射規(guī)則
1、對(duì)哪些目錄映射?
classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ /:當(dāng)前項(xiàng)目的根路徑
2、什么意思?
就我們?cè)谏厦嫖鍌€(gè)目錄下放靜態(tài)資源(比如:a.js等),可以直接訪問(http://localhost:8080/a.js),類似于以前web項(xiàng)目的webapp下;放到其他目錄下無法被訪問。
3、為什么是那幾個(gè)目錄?
3.1、看源碼
我們一起來讀下源碼,這個(gè)是SpringBoot自動(dòng)配置的WebMvcAutoConfiguration.java類來幫我們干的。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
3.2、分析源碼
我們重點(diǎn)分析后半截,前半截后面會(huì)介紹。
// staticPathPattern是/**
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
this.resourceProperties.getStaticLocations()
========>
ResourceProperties
public String[] getStaticLocations() {
return this.staticLocations;
}
========>
private String[] staticLocations = RESOURCE_LOCATIONS;
========>
private static final String[] RESOURCE_LOCATIONS;
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
========>
static {
// 可以看到如下是對(duì)上面兩個(gè)數(shù)組進(jìn)行復(fù)制操作到一個(gè)新數(shù)組上,也就是合并。
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);
}
所以上述代碼經(jīng)過我的翻譯后成為了如下樣子:
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/", "/")
// 設(shè)置緩存時(shí)間
.setCachePeriod(cachePeriod));
3.3、一句話概括
WebMvcAutoConfiguration類自動(dòng)為我們注冊(cè)了如下目錄為靜態(tài)資源目錄,也就是說直接可訪問到資源的目錄。
classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ /:當(dāng)前項(xiàng)目的根路徑
優(yōu)先級(jí)從上到下。
所以,如果static里面有個(gè)index.html,public下面也有個(gè)index.html,則優(yōu)先會(huì)加載static下面的index.html,因?yàn)閮?yōu)先級(jí)!
4、默認(rèn)首頁
PS:就是直接輸入ip:port/項(xiàng)目名稱默認(rèn)進(jìn)入的頁面。
4.1、看源碼
WebMvcAutoConfiguration.java
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
4.2、分析源碼
resourceProperties.getWelcomePage()
========>
public Resource getWelcomePage() {
// 遍歷默認(rèn)靜態(tài)資源目錄后面拼接個(gè)index.html的數(shù)組
// 比如:[/static/index.html, /public/index.html等等]
for (String location : getStaticWelcomePageLocations()) {
Resource resource = this.resourceLoader.getResource(location);
try {
if (resource.exists()) {
resource.getURL();
return resource;
}
}
catch (Exception ex) {
// Ignore
}
}
return null;
}
========>
// 下面這段代碼通俗易懂,就是給默認(rèn)靜態(tài)資源目錄后面拼接個(gè)index.html并返回,比如:/static/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īng)過我的翻譯后成為了如下樣子:
return new WelcomePageHandlerMapping( "classpath:/META-INF/resources/index.html", "classpath:/resources/index.html", "classpath:/static/index.html", "classpath:/public/index.html", "/index.html" , "/**");
4.3、一句話概括
WebMvcAutoConfiguration類自動(dòng)為我們注冊(cè)了如下文件為默認(rèn)首頁。
classpath:/META-INF/resources/index.html classpath:/resources/index.html classpath:/static/index.html classpath:/public/index.html /index.html
優(yōu)先級(jí)從上到下。
所以,如果static里面有個(gè)index.html,public下面也有個(gè)index.html,則優(yōu)先會(huì)加載static下面的index.html,因?yàn)閮?yōu)先級(jí)!
5、favicon.ico
PS:就是
這個(gè)圖標(biāo)。
5.1、看源碼
@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(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}
}
5.2、分析源碼
// 首先可以看到的是可以設(shè)置是否生效,通過參數(shù)spring.mvc.favicon.enabled來配置,若無此參數(shù),則默認(rèn)是生效的。
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
========》
// 可以看到所有的**/favicon.ico都是在faviconRequestHandler()這個(gè)方法里找。
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
========》
faviconRequestHandler().this.resourceProperties.getFaviconLocations()
// 就是之前的五個(gè)靜態(tài)資源文件夾。
List<Resource> getFaviconLocations() {
List<Resource> locations = new ArrayList<Resource>(
this.staticLocations.length + 1);
if (this.resourceLoader != null) {
for (String location : this.staticLocations) {
locations.add(this.resourceLoader.getResource(location));
}
}
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
}
5.3、一句話概括
只要把favicon.ico放到如下目錄下,就會(huì)自動(dòng)生效。
classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ /:當(dāng)前項(xiàng)目的根路徑
6、webjars
6.1、看源碼
WebMvcAutoConfiguration
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
6.2、分析源碼
這次我們來分析前半截。
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
6.3、一句話概括
所有/webjars/**都從classpath:/META-INF/resources/webjars/路徑下去找對(duì)應(yīng)的靜態(tài)資源。
6.4、什么是webjars?
就是以jar包的方式引入靜態(tài)資源。
官網(wǎng)地址:http://www.webjars.org/。類似于maven倉庫。

我們可以做個(gè)例子,將jquery引入到項(xiàng)目中
<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version> </dependency>
看項(xiàng)目依賴

會(huì)自動(dòng)為我們引入jquery,要怎么使用呢?我們上面說過:
所有/webjars/*都從classpath:/META-INF/resources/webjars/路徑下去找對(duì)應(yīng)的靜態(tài)資源。
所以我們啟動(dòng)項(xiàng)目,訪問:http://localhost:8080/webjars/jquery/3.3.1/jquery.js即可。
必須在這幾個(gè)路徑下SpringBoot才會(huì)掃描到!
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當(dāng)前項(xiàng)目的根路徑

到此這篇關(guān)于SpringBoot中的靜態(tài)資源訪問的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 靜態(tài)資源訪問內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot中實(shí)現(xiàn)接口冪等性的4種方案小結(jié)
本文主要介紹了Springboot中實(shí)現(xiàn)接口冪等性,包含數(shù)據(jù)庫的冪等,數(shù)據(jù)庫的冪等,Redis的冪等性和Token + 時(shí)間戳的冪等性,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Java?SpringBoot項(xiàng)目如何優(yōu)雅的實(shí)現(xiàn)操作日志記錄
這篇文章主要介紹了Java?SpringBoot項(xiàng)目如何優(yōu)雅的實(shí)現(xiàn)操作日志記錄,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
新浪開源輕量級(jí)分布式RPC框架motan簡單示例解析
這篇文章主要為大家介紹了新浪開源輕量級(jí)分布式RPC框架motan的簡單示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java的增強(qiáng)for循環(huán)修改數(shù)組元素的問題小結(jié)
增強(qiáng)for循環(huán)的元素變量x,就是一個(gè)局部變量,它是引用數(shù)組當(dāng)前元素引用的副本(就相當(dāng)于上文所說的你復(fù)刻朋友的鑰匙),或者是基本數(shù)據(jù)類型的值的副本,這篇文章主要介紹了Java的增強(qiáng)for循環(huán)修改數(shù)組元素的問題小結(jié),需要的朋友可以參考下2024-02-02
springboot集成mybatis-plus遇到的問題及解決方法
這篇文章主要介紹了springboot集成mybatis-plus遇到的問題及解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
redisson 實(shí)現(xiàn)分布式鎖的源碼解析
這篇文章主要介紹了redisson 實(shí)現(xiàn)分布式鎖的源碼解析,通過模擬一個(gè)商品秒殺的場景結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
詳解Java并發(fā)工具類之CountDownLatch和CyclicBarrier
在JDK的并發(fā)包中,有幾個(gè)非常有用的并發(fā)工具類,它們分別是:CountDownLatch、CyclicBarrier、Semaphore和Exchanger,本文主要來講講其中CountDownLatch和CyclicBarrier的使用,感興趣的可以了解一下2023-06-06

