SpringBoot——靜態資源映射規則

靜態資源映射

SpringBoot對於SpringMVC的自動化配置都在WebMVCAutoConfiguration類中。html

1568273415096

其中一個靜態內部類WebMvcAutoConfigurationAdapter實現了WebMvcConfigurer接口。(361)web

WebMvcConfigurer接口中定義了addResourceHandlers處理靜態資源的默認映射關係.(500)spring

1568273579633

addResourceHandlers在WebMvcAutoConfigurationAdapter類中實現瀏覽器

public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }

其中springboot

this.resourceProperties.getStaticLocations()

返回靜態資源的默認映射關係,mvc

1568274734576

getStaticLocations()方法在ResourceProperties中定義app

其中,spring-boot

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

classpath:/META-INF/resources/this

classpath:/resources/spa

classpath:/static/

classpath:/public/

第五個默認的資源映射:在靜態方法getResourceLocations中定義

1568275149287

/

小結:

默認狀況下,能夠在如下五個位置放置靜態資源

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/

【靜態資源通常放在classpath:/static/目錄下】

自定義favicon,自定義index.html

favicon.ico是瀏覽器左上角的圖標,能夠放在靜態資源路徑下或者類路徑下,靜態資源路徑優先級高。

1568275953018

1568275943433

SpringBoot啓動後默認在靜態資源目錄下尋找index.html,若是沒有找到;就會去resource/templates目錄下尋找index.html(使用Thymeleaf模板)

1568276104502

1568276090522

定製banner

http://www.network-science.de/ascii/

建立banner.txt文件置於resource目錄下

1568277030901

SpringBoot熱部署

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

1572848959987

CTRL + ALT + Shift + /

1572848974872

1572849015842

相關文章
相關標籤/搜索