0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

介紹一下單體應用中Spring Boot對靜態(tài)資源的一些映射規(guī)則

冬至子 ? 來源:瑞煕share ? 作者:brevity wit ? 2023-06-02 11:02 ? 次閱讀

在實際項目開發(fā)中,除了程序代碼外,還需要一些靜態(tài)資源,比如公司logo,背景圖,css樣式文件,js文件等等,這里介紹一下單體應用中Spring Boot對靜態(tài)資源的一些映射規(guī)則。(此處的單體應用指非前后端分離、非微服務、非SOA架構的簡易版項目,具體區(qū)別看下圖所示)

圖片

Spring Boot對靜態(tài)資源的映射規(guī)則

在Spring Boot中,SpringMVC的相關配置都默認在WebMvcAutoConfiguration類中,具體源碼請在IDE中自行搜索查看。

1、 所有/webjars/**(/**表示訪問此路徑下的任何資源,都會去classpath:/META-INF/resources/webjars/下尋找資源(webjars就是以jar包方式引入資源到項目中), 相關源碼如下:

// WebMvcAutoConfiguration.java
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
   addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) - > {
      registration.addResourceLocations(this.resourceProperties.getStaticLocations());
      if (this.servletContext != null) {
         ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
         registration.addResourceLocations(resource);
      }
   });
}
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
   addResourceHandler(registry, pattern, (registration) - > registration.addResourceLocations(locations));
}
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern,
      Consumer< ResourceHandlerRegistration > customizer) {
   if (registry.hasMappingForPattern(pattern)) {
      return;
   }
   ResourceHandlerRegistration registration = registry.addResourceHandler(pattern);
   customizer.accept(registration);
   registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));
   registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
   registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
   customizeResourceHandlerRegistration(registration);
}

結構如圖所示(以jquery為例):

1.jpg

jquery的maven依賴如下:

< dependency >
    < groupId >org.webjars.npm< /groupId >
    < artifactId >jquery< /artifactId >
    < version >3.6.0< /version >
< /dependency >

訪問示例地址如下:

localhost:8080/webjars/jquery/3.6.0/dist/jquery.js

訪問結果如下圖所示:

圖片

2、 /**,訪問當前項目下的任何靜態(tài)資源,相關源碼如下:

// WebMvcAutoConfiguration.java
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) - > {
   registration.addResourceLocations(this.resourceProperties.getStaticLocations());
   if (this.servletContext != null) {
      ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
      registration.addResourceLocations(resource);
   }
});


// WebMvcProperties.java
public String getStaticPathPattern() {
   return this.staticPathPattern;
}
private String staticPathPattern = "/**";


// WebProperties.java
public String[] getStaticLocations() {
   return this.staticLocations;
}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
      "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
 * /resources/, /static/, /public/].
 */
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

由源碼可知,靜態(tài)資源的訪問路徑有如下幾個:

1、classpath:/META-INF/resources/ ;

2、classpath:/resources/;

3、classpath:/static/;

4、classpath:/public/;

5、/ (當前項目的根路徑)。

如下圖所示:

1.jpg

**3、 **歡迎頁: 靜態(tài)資源文件夾下的index.html頁面,相關源碼如下:

// WebMvcAutoConfiguration.java

@Bean

public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,

FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {

WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(

new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),

this.mvcProperties.getStaticPathPattern());

welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));

welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());

return welcomePageHandlerMapping;

}

private Resource getWelcomePage() {

for (String location : this.resourceProperties.getStaticLocations()) {

Resource indexHtml = getIndexHtml(location);

if (indexHtml != null) {

return indexHtml;

}

}

ServletContext servletContext = getServletContext();

if (servletContext != null) {

return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));

}

return null;

}

private Resource getIndexHtml(String location) {

return 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 ex) {

}

return null;

}

// WebMvcProperties.java

public String getStaticPathPattern() {

return this.staticPathPattern;

}

private String staticPathPattern = "/**";

根據(jù)源碼可知,歡迎頁是被/**映射,也解釋了首頁名稱為index.html的原因。

4、 自定義靜態(tài)資源文件夾,在配置文件application.properties中添加如下配置,就會覆蓋掉項目的默認配置,示例代碼如下:

spring.web.resources.static-locations=classpath:/brevity/,classpath:/github/

以上就是Spring Boot單體應用中關于靜態(tài)資源映射的說明。

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • CSS
    CSS
    +關注

    關注

    0

    文章

    108

    瀏覽量

    14326
  • MVC
    MVC
    +關注

    關注

    0

    文章

    73

    瀏覽量

    13828
  • SOA技術
    +關注

    關注

    0

    文章

    4

    瀏覽量

    5573
收藏 人收藏

    評論

    相關推薦

    Spring BootDocker的入門指南()

    許多人使用容器來包裝他們的 Spring Boot 應用程序,而構建容器并不是件簡單的事情。這是針對 Spring Boot 應用程序開發(fā)
    的頭像 發(fā)表于 06-28 15:54 ?2617次閱讀

    Spring Boot虛擬線程和Webflux性能對比

    早上看到篇關于Spring Boot虛擬線程和Webflux性能對比的文章,覺得還不錯。內容較長,抓重點給大家介紹一下這篇文章的核心內容,
    發(fā)表于 09-24 14:54 ?802次閱讀
    <b class='flag-5'>Spring</b> <b class='flag-5'>Boot</b>虛擬線程和Webflux性能對比

    Spring Boot Starter需要什么

    pulsar-spring-boot-starter是非常有必要的,在此之前,我們先看看個starter需要什么。 Spring Boot
    的頭像 發(fā)表于 09-25 11:35 ?687次閱讀
    <b class='flag-5'>Spring</b> <b class='flag-5'>Boot</b> Starter需要<b class='flag-5'>些</b>什么

    啟動Spring Boot項目應用的三種方法

    來啟動。如下圖所示。第三種、java命令jar文件啟動。首先回到項目文件夾把這個項目編譯一下:mvn install,編譯完成后切換到target目錄:cd target,發(fā)現(xiàn)多了個jar后綴
    發(fā)表于 01-14 17:33

    PCB布板一些簡易常用規(guī)則

    PCB布板一些簡易常用規(guī)則   這幾天還是關注一些簡單入門的東西吧,主要介紹一些PCB中
    發(fā)表于 11-21 14:34 ?6603次閱讀

    Spring Boot特有的實踐

    Spring Boot是最流行的用于開發(fā)微服務的Java框架。在本文中,我將與你分享自2016年以來我在專業(yè)開發(fā)中使用Spring Boot所采用的最佳實踐。這些內容是基于我的個人經(jīng)驗
    的頭像 發(fā)表于 09-29 10:24 ?843次閱讀

    Spring Boot Web相關的基礎知識

    Boot的第個接口。接下來將會將會介紹使用Spring Boot開發(fā)Web應用的相關內容,其主要包括使用
    的頭像 發(fā)表于 03-17 15:03 ?592次閱讀

    簡述Spring Boot數(shù)據(jù)校驗

    篇文章我們了解了Spring Boot Web相關的知識,初步了解了spring-boot-starter-web,還了解了@Contrler和@RestController的差別
    的頭像 發(fā)表于 03-17 15:07 ?707次閱讀

    Spring Boot如何使用定時任務

    本文介紹Spring Boot 如何使用定時任務,使用非常簡單,就不做過多說明了。
    的頭像 發(fā)表于 04-12 10:56 ?915次閱讀

    Spring Boot靜態(tài)資源映射規(guī)則

    在實際項目開發(fā),除了程序代碼外,還需要一些靜態(tài)資源,比如公司logo,背景圖,css樣式文件,js文件等等
    的頭像 發(fā)表于 06-02 10:04 ?910次閱讀
    <b class='flag-5'>Spring</b> <b class='flag-5'>Boot</b>對<b class='flag-5'>靜態(tài)</b><b class='flag-5'>資源</b>的<b class='flag-5'>映射</b><b class='flag-5'>規(guī)則</b>

    Spring Boot Actuator快速入門

    一下 Spring Boot Actuator ,學習如何在 Spring Boot 2.x 中使用、配置和擴展這個監(jiān)控工具。
    的頭像 發(fā)表于 10-09 17:11 ?570次閱讀

    Spring Boot啟動 Eureka流程

    在上篇已經(jīng)說過了 Eureka-Server 本質上是個 web 應用的項目,今天就來看看 Spring Boot 是怎么啟動 Eureka 的。
    的頭像 發(fā)表于 10-10 11:40 ?785次閱讀
    <b class='flag-5'>Spring</b> <b class='flag-5'>Boot</b>啟動 Eureka流程

    Spring Boot的啟動原理

    來指定依賴,才能夠運行。我們今天就來分析講解一下 Spring Boot 的啟動原理。 1. Spring Boot 打包插件
    的頭像 發(fā)表于 10-13 11:44 ?581次閱讀
    <b class='flag-5'>Spring</b> <b class='flag-5'>Boot</b>的啟動原理

    Spring Boot 的設計目標

    Spring 框架復雜的XML配置。使用 Spring Boot 可以很容易創(chuàng)建個獨立運行的、基于 Spring 的生產級應用程序,而且
    的頭像 發(fā)表于 10-13 14:56 ?519次閱讀
    <b class='flag-5'>Spring</b> <b class='flag-5'>Boot</b> 的設計目標

    Spring Boot 3.2支持虛擬線程和原生鏡像

    Spring Boot 3.2 前幾日發(fā)布,讓我們用 Java 21、GraalVM 和虛擬線程來嘗試一下。
    的頭像 發(fā)表于 11-30 16:22 ?628次閱讀