介绍一下单体应用中Spring Boot对静态资源的一些映射规则

在实际项目开发中,除了程序代码外,还需要一些静态资源,比如公司logo,背景图,css样式文件,js文件等等,这里介绍一下单体应用中spring boot对静态资源的一些映射规则。(此处的单体应用指非前后端分离、非微服务、非soa架构的简易版项目,具体区别看下图所示)
spring boot对静态资源的映射规则
在spring boot中,springmvc的相关配置都默认在webmvcautoconfiguration类中,具体源码请在ide中自行搜索查看。
1、 所有/webjars/**(/**表示访问此路径下的任何资源,都会去classpath:/meta-inf/resources/webjars/下寻找资源(webjars就是以jar包方式引入资源到项目中), 相关源码如下:
// webmvcautoconfiguration.java@overridepublic 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 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为例):
jquery的maven依赖如下:
org.webjars.npm jquery 3.6.0访问示例地址如下:
localhost:8080/webjars/jquery/3.6.0/dist/jquery.js访问结果如下图所示:
2、 /**,访问当前项目下的任何静态资源,相关源码如下:
// webmvcautoconfiguration.javaaddresourcehandler(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.javapublic string getstaticpathpattern() { return this.staticpathpattern;}private string staticpathpattern = /**;// webproperties.javapublic 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;由源码可知,静态资源的访问路径有如下几个:
1、classpath:/meta-inf/resources/ ;
2、classpath:/resources/;
3、classpath:/static/;
4、classpath:/public/;
5、/ (当前项目的根路径)。
如下图所示:
**3、 **欢迎页: 静态资源文件夹下的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 = /**;
根据源码可知,欢迎页是被/**映射,也解释了首页名称为index.html的原因。
4、 自定义静态资源文件夹,在配置文件application.properties中添加如下配置,就会覆盖掉项目的默认配置,示例代码如下:
spring.web.resources.static-locations=classpath:/brevity/,classpath:/github/以上就是spring boot单体应用中关于静态资源映射的说明。

受日本禁令冲击,三星晶圆代工恐难如期交货
巨头扎堆布局VR生态 盘点2023 CESVR产品
荣耀5G双模手机性能分析,超高跑分的旗舰机
自动驾驶技术中单车智能VS智能网联之争你怎么看
意法半导体推出超低功耗的工业资产管理
介绍一下单体应用中Spring Boot对静态资源的一些映射规则
常见的可燃气体探测器的使用范围
联发科10核处理器Helio X30正式商用
微软与SAS宣布了在分析和AI领域的技术合作伙伴关系
华为官网2018年上半年业绩全文
汽车排气管三维扫描3D逆向建模设计服务解决方案
诺基亚5G创纪录,Meta加速元宇宙
智能手表销量下滑的背后 统一规则与差异化发展
用单细胞多组学解码生命奥秘,开发一站式单细胞测序解决方案
珠海能否成为第二个深圳?
有什么方法可以加速和优化PCB故障排除
因疫情影响,音圈电机iPhone13备货或受影响
搭载华为芯片的无人机将面世 谁是幕后推手?
高容量长待机手机电池产品将成主流
王传福在新能源领域耕耘多年亮出的一把“刀”