SpringBoot Web开发1-自动配置原理浅析

🤖🤖-摘要:
本文介绍了SpringBoot Web开发自动配置原理,包括web场景启动器的使用和自动装配功能,在web场景中添加特性如视图解析,静态资源处理,数据类型转换等。还有自定义静态资源的两种方式,通过配置文件或代码。
我的SpringBoot项目第四个模块
由前面SpringBoot快速入门分析可知,SpringBoot提出场景启动器的概念,
将场景中需要的所有依赖囊括进来,并自动装配,简化配置.
场景一引入,配置即完成
web开发同样需要web场景启动器
引入web场景启动器
<dependency> |
spring-boot-starter-web依赖核心场景启动器:spring-boot-starterspring-boot-starter依赖:spring-boot-autoconfigurespring-boot-autoconfigure会加载:META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
中的自动配置类
其中web相关有如下:
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration |
上面的自动配置类会绑定的配置有:
server==>服务器相关spring.mvc==>springMVC相关server.servlet.encoding==>servlet编码相关spring.servlet.multipart==>servlet文件处理相关
SpringBoot 的自动装配功能在web场景中添加了如下特性:
官网介绍:
- Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
- Support for serving static resources, including support for WebJars (covered later in this document).
- Automatic registration of Converter, GenericConverter, and Formatter beans.
- Support for HttpMessageConverters (covered later in this document).
- Automatic registration of MessageCodesResolver (covered later in this document).
- Static index.html support.
- Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
If you want to keep those Spring Boot MVC customizations and make more
MVC customizations (interceptors, formatters, view controllers, and other features),
you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.If you want to provide custom instances of RequestMappingHandlerMapping,
RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver,
and still keep the Spring Boot MVC customizations, you can declare a bean of
type WebMvcRegistrations and use it to provide custom instances of those components.If you want to take complete control of Spring MVC,
you can add your own @Configuration annotated with @EnableWebMvc,
or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration
as described in the Javadoc of @EnableWebMvc
web场景引入后,就有了如下功能:
- 包含
ContentNegotiatingViewResolver和BeanNameViewResolver组件,方便视图解析 - 默认的静态资源处理机制: 静态资源放在static文件夹下即可直接访问
- 自动注册了
Converter,GenericConverter,Formatter组件,适配常见数据类型转换和格式化需求 - 支持
HttpMessageConverters,可以方便返回json等数据类型 - 自动注册
MessageCodesResolver,方便国际化及错误消息处理 - 支持静态 index.html
- 自动使用
ConfigurableWebBindingInitializer,实现消息处理,数据绑定,类型转化,数据校验等功能
注意:
- 如果想保持 SpringBoot MVC 的默认配置,并且自定义更多的 MVC 配置,如:
interceptors,formatters,view controllers
等.可以使用@Configuration注解添加一个WebMvcConfigurer类型的配置类,但不要标注@EnableWebMvc - 如果想保持 SpringBoot MVC
的默认配置,但要自定义核心组件实例,比如:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
,或ExceptionHandlerExceptionResolver,给容器中放一个WebMvcRegistrations组件即可 - 如果想全面接管 Spring MVC,
@Configuration标注一个配置类,并加上@EnableWebMvc注解,实现WebMvcConfigurer接口
接下来分别展开分析.
自动配置原理浅析
由前面知道引入web场景会加载自动配置类:WebMvcAutoConfiguration
下面简要分析WebMvcAutoConfiguration原理
生效条件
|
生效后, 在容器中放入两个bean:
HiddenHttpMethodFilter: 过滤页面表单提交的Rest请求
- Rest 请求有:
GET,POST,PUT,DELETE,PATCH- 请求数据包含三部分: 请求行, 请求头, 请求体
- 浏览器只能发送
GET请求和POST请求GET请求没有请求体, 参数在请求行中,获取参数的方法是getQueryString()POST请求的参数在请求体中, 获取方法是getReader()或getInputStream()- getMethod() 获取具体哪种请求方式
/** |
FormContentFilter:用于分析表单内容, 与前面的过滤器配合使用,
同样只针对PUT,PATCH,DELETE三种HTTP请求
静态内部类WebMvcAutoConfigurationAdapter
在WebMvcAutoConfiguration中有静态内部类WebMvcAutoConfigurationAdapter源码如下:
// Defined as a nested config to ensure WebMvcConfigurer is not read when not on the classpath |
接口WebMvcConfigurer
WebMvcAutoConfigurationAdapter实现了WebMvcConfigurer接口,可以重写一些有关Web MVC的配置方法,比如添加拦截器、配置视图解析器、配置静态资源等.
通过重写这些方法,可以根据自己的需要定制化Web MVC的行为.可定制功能有:

- addArgumentResolvers:添加参数解析器
- addCorsMappings:添加跨域映射
- addFormatters:添加格式化器
- addInterceptors:添加拦截器
- addResourceHandlers:添加资源处理器,处理静态资源规则
- addReturnValueHandlers:添加返回值处理器
- addViewControllers:添加视图控制器,指定某个请求路径跳转到指定页面
- configureAsyncSupport:配置异步支持
- configureContentNegotiation:配置内容协商
- configureDefaultServletHandling:配置默认的处理,默认接收: /
- configureHandlerExceptionResolvers:配置异常解析器
- configureMessageConverters:配置消息转化器
- configurePathMatch:配置路径匹配
- configureViewResolvers:配置视图解析器
- extendHandlerExceptionResolvers:扩展处理异常解析器
- extendMessageConverters扩展消息转换器
- getMessageCodesResolver:获取消息编码解析器
- getValidator:获取校验器
静态资源规则源码浅析
由上面分析,addResourceHandlers用来处理静态资源,源码:
private static final String SERVLET_LOCATION="/"; |
/** |
小结一下:
静态资源访问规则如下:
- 规则一 访问:
/webjars/**路径就去classpath:/META-INF/resources/webjars/下找资源. - 规则二 访问:
/**路径就去 静态资源默认的四个位置找资源classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/
- 规则三 静态资源默认都有缓存规则的设置
- 所有缓存的设置, 直接通过配置文件:
spring.web - cachePeriod: 缓存周期; 多久不用找服务器要新的, 默认没有,以秒为单位
- cacheControl: HTTP缓存控制, 查看文档
- useLastModified:是否使用最后一次修改, 配合HTTP Cache规则, 如果浏览器访问了一个静态资源
- 所有缓存的设置, 直接通过配置文件:
如果浏览器访问了一个静态资源 index.js,如果服务这个资源没有发生变化,下次访问的时候就可以直接让浏览器用自己缓存中的东西,而不用给服务器发请求
HTTP缓存实验
设置如下:
#1、spring.web: |
第一次请求:

第二次请求:

自定义静态资源
大体分为两种方式:
- 配置文件的方式
- 代码的方式
配置文件
|
与两个配置文件绑定WebMvcProperties.class和WebProperties.class,
即以spring.web和spring.mvc开头的配置
spring.web:可配置locale(国际化)和resources(静态资源相关),具体可查看WebProperties.classspring.mvc:可配置内容很多,具体可查看WebMvcProperties.class
spring: |
代码方式
就是在容器中放置组件:WebMvcConfigurer,来配置底层.
注意:
- 默认配置仍有效
- 加上
@EnableWebMvc会将默认配置失效
|
|
为什么容器中含有WebMvcConfigurer组件,就能配置底层行为???
WebMvcAutoConfiguration是一个自动配置类, 它里面有一个EnableWebMvcConfigurationEnableWebMvcConfiguration继承与DelegatingWebMvcConfiguration, 这两个都生效DelegatingWebMvcConfiguration利用DI把容器中所有WebMvcConfigurer注入进来- 当调用
DelegatingWebMvcConfiguration的方法配置底层规则, 而它调用所有WebMvcConfigurer的配置底层方法