在實(shí)際項(xiàng)目開發(fā)中,除了程序代碼外,還需要一些靜態(tài)資源,比如公司logo,背景圖,css樣式文件,js文件等等,這里介紹一下單體應(yīng)用中Spring Boot對靜態(tài)資源的一些映射規(guī)則。(此處的單體應(yīng)用指非前后端分離、非微服務(wù)、非SOA架構(gòu)的簡易版項(xiàng)目,具體區(qū)別看下圖所示)
Spring Boot對靜態(tài)資源的映射規(guī)則
在Spring Boot中,SpringMVC的相關(guān)配置都默認(rèn)在WebMvcAutoConfiguration類中,具體源碼請?jiān)贗DE中自行搜索查看。
1、 所有/webjars/**(/**表示訪問此路徑下的任何資源,都會(huì)去classpath:/META-INF/resources/webjars/下尋找資源(webjars就是以jar包方式引入資源到項(xiàng)目中), 相關(guān)源碼如下:
// WebMvcAutoConfiguration.java
@Override
public 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< ResourceHandlerRegistration > 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);
}
結(jié)構(gòu)如圖所示(以jquery為例):
jquery的maven依賴如下:
< dependency >
< groupId >org.webjars.npm< /groupId >
< artifactId >jquery< /artifactId >
< version >3.6.0< /version >
< /dependency >
訪問示例地址如下:
localhost:8080/webjars/jquery/3.6.0/dist/jquery.js
訪問結(jié)果如下圖所示:
2、 /**,訪問當(dāng)前項(xiàng)目下的任何靜態(tài)資源,相關(guān)源碼如下:
// WebMvcAutoConfiguration.java
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);
}
});
// WebMvcProperties.java
public String getStaticPathPattern() {
return this.staticPathPattern;
}
private String staticPathPattern = "/**";
// WebProperties.java
public 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;
由源碼可知,靜態(tài)資源的訪問路徑有如下幾個(gè):
1、classpath:/META-INF/resources/ ;
2、classpath:/resources/;
3、classpath:/static/;
4、classpath:/public/;
5、/ (當(dāng)前項(xiàng)目的根路徑)。
如下圖所示:
**3、 **歡迎頁: 靜態(tài)資源文件夾下的index.html頁面,相關(guān)源碼如下:
// 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 = "/**";
根據(jù)源碼可知,歡迎頁是被/**映射,也解釋了首頁名稱為index.html的原因。
4、 自定義靜態(tài)資源文件夾,在配置文件application.properties中添加如下配置,就會(huì)覆蓋掉項(xiàng)目的默認(rèn)配置,示例代碼如下:
spring.web.resources.static-locations=classpath:/brevity/,classpath:/github/
以上就是Spring Boot單體應(yīng)用中關(guān)于靜態(tài)資源映射的說明。
-
CSS
+關(guān)注
關(guān)注
0文章
110瀏覽量
14753 -
MVC
+關(guān)注
關(guān)注
0文章
73瀏覽量
14070 -
SOA技術(shù)
+關(guān)注
關(guān)注
0文章
4瀏覽量
5644
發(fā)布評論請先 登錄
Spring Boot中Docker的入門指南(一)
Spring Boot虛擬線程和Webflux性能對比

Spring Boot Starter需要些什么

啟動(dòng)Spring Boot項(xiàng)目應(yīng)用的三種方法
PCB布板一些簡易常用規(guī)則
Spring Boot特有的實(shí)踐
Spring Boot Web相關(guān)的基礎(chǔ)知識
簡述Spring Boot數(shù)據(jù)校驗(yàn)
在Spring Boot中如何使用定時(shí)任務(wù)
Spring Boot對靜態(tài)資源的映射規(guī)則

Spring Boot Actuator快速入門
Spring Boot啟動(dòng) Eureka流程

Spring Boot 的設(shè)計(jì)目標(biāo)

評論