静态资源映射

SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中。

其中一个静态内部类WebMvcAutoConfigurationAdapter实现了WebMvcConfigurer接口。(361)

WebMvcConfigurer接口中定义了addResourceHandlers处理静态资源的默认映射关系.(500)

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));
} }
}

其中

this.resourceProperties.getStaticLocations()

返回静态资源的默认映射关系,

getStaticLocations()方法在ResourceProperties中定义

其中,

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

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

第五个默认的资源映射:在静态方法getResourceLocations中定义

/

小结:

默认情况下,可以在以下五个位置放置静态资源

classpath:/META-INF/resources/

classpath:/resources/

classpath:/static/

classpath:/public/

/

【静态资源一般放在classpath:/static/目录下】

自定义favicon,自定义index.html

favicon.ico是浏览器左上角的图标,可以放在静态资源路径下或者类路径下,静态资源路径优先级高。

SpringBoot启动后默认在静态资源目录下寻找index.html,如果没有找到;就会去resource/templates目录下寻找index.html(使用Thymeleaf模板)

定制banner

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

创建banner.txt文件置于resource目录下

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>

CTRL + ALT + Shift + /

最新文章

  1. c3p0 连接池
  2. Swift:subscript
  3. usaco 2016 Feb 负载平衡
  4. C#编程利器之一:类(Class)【转】
  5. Maven打包pom里面配置exclude 排除掉环境相关的配置文件
  6. Lambda表达式的演化,委托-匿名方法-Func-Lambda
  7. poi导出Excel报表多表头双层表头、合并单元格
  8. spark HA 安装配置和使用(spark1.2-cdh5.3)
  9. 语义Web和本体开发相关技术
  10. [wikioi]乌龟棋
  11. 转 常用JQuery插件整理
  12. Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别
  13. .net杂记
  14. Linux环境下的GCC编译器与GDB调试工具介绍
  15. 【BZOJ4013】[HNOI2015]实验比较(动态规划)
  16. Python 3 Anaconda 下爬虫学习与爬虫实践 (2)
  17. HDU6205
  18. JAVA 远程通讯机制
  19. KVM -&gt; 虚拟机管理&amp;console登录_02
  20. Maven 那些破事

热门文章

  1. python实现RSA加密和签名以及分段加解密的方案
  2. Hey Future!
  3. Spring-Boot:拦截器注解范例
  4. Freemarker提供了3种加载模板目录的方法
  5. 缓存系列-Redis入门教程
  6. Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量
  7. egret之纹理填充模式(上下填充)
  8. Javaweb Cookie机制
  9. 重识 ArrayList
  10. [python]python字典