SpringBoot——静态资源及原理
一、使用 SpringBoot 的步骤
【1】创建 SpringBoot应用,选中自己需要的模块。
【2】SpringBoot 已经默认将这些场景配置好,只需要在配置文件中指定少量配置就可以运行起来。
【3】编写业务逻辑代码。
二、自动配置原理
我们要了解 SpringBoot帮我们配置了什么?能不能修改?能修改那些配置?能不能扩展等等。
【1】xxxAutoConfiguration:帮我们给容器中自动配置组件。
【2】xxxProperties:配置来封装配置文件的内容。
三、SpringBoot 对静态资源的映射规则
当创建一个 jar工程时,想引入 css等静态资源时,需要遵守 SpringBoot的静态资源映射关系,通过 WebMvcAutoConfiguration查看静态配置资源的规则。
1 //添加资源映射addResourceHandlers
2 public void addResourceHandlers(ResourceHandlerRegistry registry) {
3 if(!this.resourceProperties.isAddMappings()) {
4 logger.debug("Default resource handling disabled");
5 } else {
6 Integer cachePeriod = this.resourceProperties.getCachePeriod();
7 if(!registry.hasMappingForPattern("/webjars/**")) {
8 this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
9 }
10 // 3中说明
11 String staticPathPattern = this.mvcProperties.getStaticPathPattern();
12 if(!registry.hasMappingForPattern(staticPathPattern)) {
13 this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
14 }
15 }
16 }
【1】如上配置的 /webjars/** 可知,所有的获取都去 classpath:/META-INF/resources/webjars 下找资源。而 webjar实际上是以 jar包的方式引入静态资源,可以参考官方文档:http://www.webjars.org/
获取 Jquery的 jar包依赖:
1 <dependency>
2 <groupId>org.webjars</groupId>
3 <artifactId>jquery</artifactId>
4 <version>3.3.1-1</version>
5 </dependency>
进入导入的 Jquery的 jar包中,查看目录结构如下:所有的 /webjars/**,都去 classpath:/META‐INF/resources/webjars/找资源。例如:localhost:8080/webjars/jquery/3.3.1/jquery.js(在访问的时候,只需要写webjars下面资源的名称即可)
【2】同时可以在 ResourceProperties设置与静态资源有关的参数,例如缓存时间。
1 @ConfigurationProperties(
2 prefix = "spring.resources",
3 ignoreUnknownFields = false
4 )
5 public class ResourceProperties implements ResourceLoaderAware, InitializingBean {
【3】除了 /webjars/**,我们看下紧接着的第二个方法获取 staticPathPattern 路径:最终方法指向 =“/**” 访问当前项目的任何资源(静态资源的文件夹)。
this.staticPathPattern = "/**";
如果没有进行处理,就会从如下路径中进行获取: classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" "/":当前项目根路径 ,以上就是静态资源的文件处理。
1 private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};//当前项目根路径
2 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/",
3 "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
4 private static final String[] RESOURCE_LOCATIONS;
5 static {
6 RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length + SERVLET_RESOURCE_LOCATIONS.length];
7 System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0, SERVLET_RESOURCE_LOCATIONS.length);
8 System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, SERVLET_RESOURCE_LOCATIONS.length,
9 CLASSPATH_RESOURCE_LOCATIONS.length);
10 }
【4】获取欢迎页,通过如下代码可知:静态资源文件夹下的所有 index.html页面,都被“/**”映射。(localhost:8080——就能够访问首页)
1 // 获取欢迎页
2 @Bean
3 public WebMvcAutoConfiguration.WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {
4 return new WebMvcAutoConfiguration.WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
5 this.mvcProperties.getStaticPathPattern());
6 }
7
8 //进入如上的resourceProperties.getWelcomePage()方法,会获取到当前项目路径下的index.html文件。
9 private String[] getStaticWelcomePageLocations() {
10 String[] result = new String[this.staticLocations.length];
11
12 for(int i = 0; i < result.length; ++i) {
13 String location = this.staticLocations[i];
14 if(!location.endsWith("/")) {
15 location = location + "/";
16 }
17
18 result[i] = location + "index.html";
19 }
20
21 return result;
22 }
23
24 //进入如上的this.mvcProperties.getStaticPathPattern()方法,获取映射的路径
25 this.staticPathPattern = "/**";
【5】所有的 **/favicon.ico 都是从静态文件中获取一个 favicon.ico文件。图标:
1 @Configuration
2 @ConditionalOnProperty(
3 value = {"spring.mvc.favicon.enabled"},
4 matchIfMissing = true
5 )
6 public static class FaviconConfiguration {
7 private final ResourceProperties resourceProperties;
8
9 public FaviconConfiguration(ResourceProperties resourceProperties) {
10 this.resourceProperties = resourceProperties;
11 }
12
13 @Bean
14 public SimpleUrlHandlerMapping faviconHandlerMapping() {
15 SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
16 mapping.setOrder(-2147483647);
17 //默认获取图标的位置和名称
18 mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
19 return mapping;
20 }
21 }
【6】也可以在 application.properties全局配置文件中自定义静态文件:在配置文件中设置如下,那么默认的就不在生效。
1 #配置文件是一个数组,可以用逗号进行分隔
2 spring.resources.static-locations=classpath:/hello/,calsspath:/xxx/
SpringBoot——静态资源及原理的更多相关文章
- SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆
在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...
- springboot静态资源映射
springboot静态资源映射 WebMvcAutoConfiguration @Override public void addResourceHandlers(ResourceHandlerRe ...
- springboot 静态资源访问,和文件上传 ,以及路径问题
springboot 静态资源访问: 这是springboot 默认的静态资源访问路径 访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...
- Spring Boot 静态资源访问原理解析
一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...
- springboot静态资源处理
转:https://blog.csdn.net/catoop/article/details/50501706 Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConf ...
- SpringBoot静态资源目录
在web开发中,静态资源的访问是必不可少的,如:图片.js.css 等资源的访问. SpringBoot对静态资源访问提供了很好的支持,基本使用默认配置就能满足开发需求. 在传统的web项目中静态资源 ...
- 聊聊、SpringBoot 静态资源访问
SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的.这篇文章就来讲讲这方面问题,也是项目中的坑. 先 ...
- springmvc、springboot静态资源访问配置
如何访问项目中的静态资源? 一.springmvc springmvc中访问静态资源,如果DispatcherServlet拦截的为"",那么静态资源的访问也会交给Dispatch ...
- SpringBoot 静态资源的配置
springboot默认的静态资源目录: classpath:/static classpath:/public classpath:/resources classpath:/META-INF/re ...
- springboot静态资源映射规则
一.所有/webjars/**的请求,都会去classpath:/META-INF/resources/webjars/下的目录去找资源. 二.访问/**,即访问任何资源,如果没有controller ...
随机推荐
- BZOJ1008 [HNOI2008]越狱 (快速幂,组合)
题目大意 求\(m\)种数字组成的长度为\(n\)的序列的种数,序列中至少有一段连续的数字 分析 用可重排列的种数减去,相邻数字互不相同的序列种数 考虑相邻互不相同,第一个元素有\(m\)种可能,后面 ...
- vue父子件,子件页面table数据列按条件显示不同的内容
需求:在父件中点击按钮.子件弹框中,table列根据条件显示不同的数据 实现思路:点击按钮,执行不同的方法,方法中参数值不同,从而展示不同的columns. 父件按钮如下图: 父件中导入子件需要注意的 ...
- Android中的特殊权限
AndroidManifest中定义的权限分为普通权限,危险权限和特殊权限. 普通权限指的是不会威胁到用户的安全和隐私的权限,只需要在AndroidManifest中声明一下就能直接使用. 危险权限指 ...
- uniapp里面设置onlaunch以后在加载页面调接口
main.js 里面 Vue.prototype.$onLaunched = new Promise(resolve => { Vue.prototype.$isResolve = resolv ...
- 4组-Beta冲刺-5/5
一.基本情况 队名:摸鲨鱼小队 组长博客:https://www.cnblogs.com/smallgrape/p/15608986.html github链接:https://github.com/ ...
- 4组-Alpha冲刺-5/6
一.基本情况 队名:摸鲨鱼小队 组长博客:https://www.cnblogs.com/smallgrape/p/15563236.html 小组人数:8人 二.冲刺概况汇报 组长:许雅萍 过去两天 ...
- Monterey 12.3 I225-V有线网卡导致死机或无法使用问题
问题:升级Monterey12.3后启动到桌面死机 解决方法: 1.关闭/去掉所有有线网卡驱动.补丁.网卡设备ID注入: 2.启动命令中加入:dk.e1000=0: 3.启动不死机后,进入网络设置,手 ...
- redis 常用指令
redis指令有些相似,记忆起来不太容易,在此做一下整理 序号 类型 指令 参数 作用 例子 1 string set key value 存储一个 string 类型的值 set a aa 2 st ...
- django中使用autocomplete无效查错
检查autocomplete是否工作正常,将自己的结果集注释掉,使用前端预设好的结果集var countries=["Afghanistan","Albania" ...
- 纯js实现字符串formate方法
function format(pattern){ if(! (pattern instanceof String)){ throw new TypeError("错误的参数类型" ...