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.ico是浏览器左上角的图标,可以放在静态资源路径下或者类路径下,静态资源路径优先级高。
springboot启动后默认在静态资源目录下寻找,如果没有找到;就会去resource/templates目录下寻找(使用thymeleaf模板)
创建banner.txt文件置于resource目录下
https://github.com/hcj-shadow/springbootplus
如对本文有疑问, 点击进行留言回复!!
集合框架——Map、泛型以及Collection算法常用方法
Elasticsearch 升级 7.x 版本后,我感觉掉坑里了
Mybatis的插件运行原理以及如何编写一个Mybatis的插件
网友评论