SpringBoot:Web开发探究

1. Web开发探究

1.简介

SpringBoot与Web开发

SpringBoot最大的特点就是自动装配,所以springboot的东西用起来非常简单

使用SpringBoot的步骤:

  1. 创建一个SpringBoot应用,选择我们需要的模块,SpringBoot就会默认将我们需要的模块自动装配好。
  2. 手动在配置文件中配置部分配置项目就可以运行起来了。
  3. 专注编写业务代码,不用考虑以前那样一大堆的配置了

要熟悉掌握开发,SpringBoot的自动装配原理一定要搞明白

比如SpringBoot到底帮忙配置了什么?我们能不能修改?能修改哪些配置?能不能扩展

  • 向容器中自动配置组件:*****Autoconfiguration
  • 自动配置类,封装配置文件内容:*****Properties

2.静态资源处理

1.静态资源映射规则

首先我们搭建一个普通的SpringBoot项目,HelloWord程序

写请求,我们要引入前端资源,我们项目中有许多前端资源,比如css,js等文件,这些前端的静态资源SpringBoot是怎么处理的呢?

当我们是web应用时,我们main下面会有一个webapp,我们以前都是将所有的页面导在这里面。

我们现在使用的为pom,打包方式为jar,这种方式SpringBoot能不能写页面呢?

SpringBoot对于静态资源放置的位置,是有规定的。

什么是静态资源映射规则?

SpringBoot中,SpringMVC的web配置都在WebMvcAutoConfiguration这个配置类里面

我们可以看看WebMvcAutoConfiguration中有很多配置方法。

addResourceHandlers方法:添加资源处理

@Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
          //已禁止默认资源处理 logger.debug(
"Default resource handling disabled"); return; }
        //缓存控制 Duration cachePeriod
= this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        //webjars 配置
if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); }
        //静态资源配置 String staticPathPattern
= this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } }

 

源码分析:所有的/webjars/**,都需要去classpath:/META-INF/resources/webjars/找对应的资源

什么是webjars?

webjars的本质就是以jar包的形式引入静态资源,我们以前导入静态资源,直接导入即可

12132312

posted @ 2020-08-21 15:50  罗晓峥  阅读(134)  评论(0编辑  收藏  举报