1. 程式人生 > 程式設計 >SpringBoot中的靜態資源訪問的實現

SpringBoot中的靜態資源訪問的實現

一、說在前面的話

我們之間介紹過SpringBoot自動配置的原理,基本上是如下:

xxxxAutoConfiguration:幫我們給容器中自動配置元件;
xxxxProperties:配置類來封裝配置檔案的內容;

二、靜態資源對映規則

1、對哪些目錄對映?

classpath:/META-INF/resources/ 
classpath:/resources/
classpath:/static/ 
classpath:/public/
/:當前專案的根路徑

2、什麼意思?

就我們在上面五個目錄下放靜態資源(比如:a.js等),可以直接訪問(http://localhost:8080/a.js),類似於以前web專案的webapp下;放到其他目錄下無法被訪問。

3、為什麼是那幾個目錄?

3.1、看原始碼

我們一起來讀下原始碼,這個是SpringBoot自動配置的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、分析原始碼

我們重點分析後半截,前半截後面會介紹。

// 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 {
  // 可以看到如下是對上面兩個陣列進行復制操作到一個新陣列上,也就是合併。
  RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
      + SERVLET_RESOURCE_LOCATIONS.length];
  System.arraycopy(SERVLET_RESOURCE_LOCATIONS,RESOURCE_LOCATIONS,SERVLET_RESOURCE_LOCATIONS.length);
  System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS,SERVLET_RESOURCE_LOCATIONS.length,CLASSPATH_RESOURCE_LOCATIONS.length);
}
所以上述程式碼經過我的翻譯後成為了如下樣子:

registry.addResourceHandler("/**").addResourceLocations(
  "classpath:/META-INF/resources/","classpath:/public/","/")
  // 設定快取時間
  .setCachePeriod(cachePeriod));

3.3、一句話概括

WebMvcAutoConfiguration類自動為我們註冊瞭如下目錄為靜態資源目錄,也就是說直接可訪問到資源的目錄。

classpath:/META-INF/resources/ 
classpath:/resources/
classpath:/static/ 
classpath:/public/
/:當前專案的根路徑

優先順序從上到下。

所以,如果static裡面有個index.html,public下面也有個index.html,則優先會載入static下面的index.html,因為優先順序!

4、預設首頁

PS:就是直接輸入ip:port/專案名稱預設進入的頁面。

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() {
  // 遍歷預設靜態資源目錄後面拼接個index.html的陣列
  // 比如:[/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;
}
========>
// 下面這段程式碼通俗易懂,就是給預設靜態資源目錄後面拼接個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;
}

所以上述程式碼經過我的翻譯後成為了如下樣子:

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類自動為我們註冊瞭如下檔案為預設首頁。

classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html 
classpath:/public/index.html
/index.html

優先順序從上到下。

所以,如果static裡面有個index.html,public下面也有個index.html,則優先會載入static下面的index.html,因為優先順序!

5、favicon.ico

PS:就是SpringBoot中的靜態資源訪問的實現這個圖示。

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、分析原始碼

// 首先可以看到的是可以設定是否生效,通過引數spring.mvc.favicon.enabled來配置,若無此引數,則預設是生效的。
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled",matchIfMissing = true)
========》
// 可以看到所有的**/favicon.ico都是在faviconRequestHandler()這個方法裡找。
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",faviconRequestHandler()));
========》
faviconRequestHandler().this.resourceProperties.getFaviconLocations()
// 就是之前的五個靜態資原始檔夾。  
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放到如下目錄下,就會自動生效。

classpath:/META-INF/resources/ 
classpath:/resources/
classpath:/static/ 
classpath:/public/
/:當前專案的根路徑

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/路徑下去找對應的靜態資源。

6.4、什麼是webjars?

就是以jar包的方式引入靜態資源。

官網地址:http://www.webjars.org/。類似於maven倉庫。

SpringBoot中的靜態資源訪問的實現

我們可以做個例子,將jquery引入到專案中

<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>jquery</artifactId>
  <version>3.3.1</version>
</dependency>

看專案依賴

SpringBoot中的靜態資源訪問的實現

會自動為我們引入jquery,要怎麼使用呢?我們上面說過:

所有/webjars/*都從classpath:/META-INF/resources/webjars/路徑下去找對應的靜態資源。

所以我們啟動專案,訪問:http://localhost:8080/webjars/jquery/3.3.1/jquery.js即可。

必須在這幾個路徑下SpringBoot才會掃描到!

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當前專案的根路徑

SpringBoot中的靜態資源訪問的實現

到此這篇關於SpringBoot中的靜態資源訪問的實現的文章就介紹到這了,更多相關SpringBoot 靜態資源訪問內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!