SpringBoot静态资源与首页配置实现原理深入分析
一、静态资源导入
关键源码可以看WebMvcAutoConfiguration这个类下面的addResourceHandlers方法
在这个方法中,我们有几个重点需要了解一下
1、webjars
可以理解为以maven的形式引入web的相关jar包
请求路径为/webjars/**
的,都会去classpath:/META-INF/resources/webjars/
下寻找相关的静态资源
2、静态资源映射规则
如果在项目中要使用我们自己导入的静态资源,它的映射规则是怎么样的呢,我们分析源码可以得出
以下四个路径的中存放的静态资源可以被识别,优先
resource(注意,此处是resource下面的resource文件夹)>static (默认)>public
"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
3、自定义静态资源路径
我们可以使用spring.web.resources.static-locations
在yaml文件中自定义静态资源文件的路径,例如我们限制静态文件都必须放在static目录下
也可以使用spring.mvc.static-path-pattern,当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找
spring:
web:
resources:
static-locations: classpath:/static/
mvc:
static-path-pattern: /static/**
随后我们访问一下静态资源,发现只有放在static下面可以被访问到
二、首页配置和图标
1、首页配置
springboot它会去找静态资源文件夹下的index.html
(注意不能配置spring.mvc.static-path-pattern)或者是controller处理/index
转发的页面
下面是WebMvcAutoConfiguration这个类中关于首页的相关方法
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations()); return welcomePageHandlerMapping; } private Resource getWelcomePage() { String[] var1 = this.resourceProperties.getStaticLocations(); int var2 = var1.length; for(int var3 = 0; var3 < var2; ++var3) { String location = var1[var3]; Resource indexHtml = this.getIndexHtml(location); if (indexHtml != null) { return indexHtml; } } ServletContext servletContext = this.getServletContext(); if (servletContext != null) { return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/"))); } else { return null; } } private Resource getIndexHtml(String location) { return this.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 var3) { } return null; }
2、图标
官网是说在静态资源路径下放置一个favicon.ico,spring boot就会自动识别
如图
图标加载成功 可能会因为缓存加载不出来 清除缓存多试几次就行了
到此这篇关于SpringBoot静态资源与首页配置实现原理深入分析的文章就介绍到这了,更多相关SpringBoot静态资源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Netty事件循环主逻辑NioEventLoop的run方法分析
这篇文章主要介绍了Netty事件循环主逻辑NioEventLoop的run方法分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-03-03Intelli IDEA安装Scala插件并安装Scala软件和配置环境变量的详细教程
这篇文章主要介绍了Intelli IDEA安装Scala插件并安装Scala软件和配置环境变量的详细教程,需要的朋友可以参考下2020-10-10java报错之springboot3+vue2项目web服务层报错总结
java入门学习,随手记录一下开发过程中产生的报错,有些错误是网上搜索再加上自己尝试,随手引用了一些其他人的记录,也是留给自己看的,或是希望能对其他初学者有帮助2023-06-06Mybatis事务如何跟Spring结合(数据库事务特性和Spring事务管理源码)
MyBatis与Spring的事务结合主要是通过Spring的事务管理和MyBatis的数据库操作来实现的,在本文中,我们将从数据库事务特性和Spring事务管理源码两个角度来分析MyBatis事务如何与Spring结合到一起的原理,感兴趣的朋友一起看看吧2024-01-01
最新评论