SpringBoot對於SpringMVC的自動化配置都在WebMVCAutoConfiguration類中。html
其中一個靜態內部類WebMvcAutoConfigurationAdapter實現了WebMvcConfigurer接口。(361)web
WebMvcConfigurer接口中定義了addResourceHandlers處理靜態資源的默認映射關係.(500)spring
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
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中定義
/
小結:
默認狀況下,能夠在如下五個位置放置靜態資源
classpath:/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/ /
【靜態資源通常放在classpath:/static/目錄下】
favicon.ico是瀏覽器左上角的圖標,能夠放在靜態資源路徑下或者類路徑下,靜態資源路徑優先級高。
SpringBoot啓動後默認在靜態資源目錄下尋找index.html,若是沒有找到;就會去resource/templates目錄下尋找index.html(使用Thymeleaf模板)
http://www.network-science.de/ascii/
建立banner.txt文件置於resource目錄下
<!-- 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>
CTRL + ALT + Shift + /