SpringBoot--SpringMVC自动配置
SpringMVC自动配置
1.SpringBoot官方文档对SpringMVC的默认配置:
Inclusion of
ContentNegotiatingViewResolverandBeanNameViewResolverbeans.- 自动配置了ViewResolver(视图解析器:根据方法返回值得到视图对象,试图对象决定如何渲染)
ContentNegotiatingViewResolver组合所有的视图解析器- 如何定制:我们可以给自己的容器添加一个视图解析器,自动的将其组合进来
Support for serving static resources, including support for WebJars (covered later in this document)).
- 静态资源文件夹路径,webjars
Automatic registration of
Converter,GenericConverter, andFormatterbeans.自动注册了
Converter类型转换器 :String ===> 封装进去UserFormatter格式化器 :2017-12-17 ===> Datepublic void addFormatters(FormatterRegistry registry) {
ApplicationConversionService.addBeans(registry, this.beanFactory);
}
public static void addBeans(FormatterRegistry registry, ListableBeanFactory beanFactory) {
Set<Object> beans = new LinkedHashSet();
beans.addAll(beanFactory.getBeansOfType(GenericConverter.class).values());
beans.addAll(beanFactory.getBeansOfType(Converter.class).values());
所以springboot同时也将容器中所有的
ConverterFormatterGenericConverter加入到组件中自己添加的格式化转换器,我们只需要放在容器中即可
Support for
HttpMessageConverters(covered later in this document).HttpMessageConverters : SpringMVC用来转换Http请求和相应的;(User对象转换成Json数据)
只有一个构造器给ObjectProvider赋值 ,所以参数直接从容器中获取, HttpMessageConverters 是从容器中确定,获取所有的HttpMessageConverter
private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
public WebMvcAutoConfigurationAdapter(…… ObjectProvider<HttpMessageConverters> messageConvertersProvider…………) {
this.messageConvertersProvider = messageConvertersProvider;
自己添加的HttpMessageConverter,只需要直接放入容器(@Bean、@Component)
Automatic registration of
MessageCodesResolver(covered later in this document).- 定义错误代码的生成规则
Static
index.htmlsupport.- 静态首页访问
Custom
Faviconsupport (covered later in this document).- favicon.ico
Automatic use of a
ConfigurableWebBindingInitializerbean (covered later in this document).我们可以配置一个
ConfigurableWebBindingInitializer来替换默认的;(添加到容器)初始化WebDataBinder
请求数据===JavaBean 将数据绑定成为我们要封装的对象,用到类型转换器,格式化器等等
ConfigurableWebBindingInitializer
public void initBinder(WebDataBinder binder) {
binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
if (this.directFieldAccess) {
binder.initDirectFieldAccess();
}
org.springframework.boot.autoconfigure.web:web的所有自动场景;
2. 扩展SpringMVC
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.
<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
编写一个配置类(Configuration),是WebMvcConfigurer 类型,不能标注@EnableWebMvc
@Configuration
//WebMvcConfigurer接口中有很多方法的对应的是以往Springmvc配置文件中的属性
//使用其来扩展SpringMVC的功能
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/jiat请求来到 success
registry.addViewController("/jiat").setViewName("success");
}
}
原理:
- WebMvcAutoConfiguration是SpringMVC的自动配置类
- 在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)
- 容器中所有的WebMvcConfiguration都会起作用
- 我们的配置类也会被调用
3.全面接管SpringMVC(在我们的WebMvcConfigurer配置类上加上一个@EnableWebMvc)
SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置,所有的SpringMVC模块自动配置全部失效,包括静态资源
@EnableWebMvc能使默认配置生效的原因
- 添加了@EnableWebMvc后会为容器导入一个WebMvcConfigurationSupport配置类,添加相应组件到容器中
@Import({DelegatingWebMvcConfiguration.class}) //导入一个DelegatingWebMvcConfiguration
public @interface EnableWebMvc {
//为容器添加一个WebMvcConfigurationSupport
@Configuration(
proxyBeanMethods = false
)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
- 而 WebMvcAutoConfiguration 的注解上有一个“@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})”,当存在如果容器中存在WebMvcConfigurationSupport对象时,WebMvcAutoConfiguration 自动配置失效
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//如果容器中存在WebMvcConfigurationSupport对象,则以下所有配置不生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
接管SpringMVC总结:
- @EnableWebMvc将WebMvcConfigurationSupport组件导入容器中
- WebMvcConfigurationSupport只是SpringMVC的最基本功能
- 用户可以在实现基本功能的WebMvcConfigurationSupport上定制自己的webmvc
4. 如何修改SpringBoot的默认配置
模式:
- SpingBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,没有才自动配置,如果有些组件可以多个(ViewResolver)将用户配置的和自己默认的组合起来
- 在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展
- 在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置
SpringBoot--SpringMVC自动配置的更多相关文章
- spring-boot spring-MVC自动配置
Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...
- SpringBoot源码学习系列之SpringMVC自动配置
目录 1.ContentNegotiatingViewResolver 2.静态资源 3.自动注册 Converter, GenericConverter, and Formatter beans. ...
- SpringBoot扩展SpringMVC自动配置
SpringBoot中自动配置了 ViewResolver(视图解析器) ContentNegotiatingViewResolver(组合所有的视图解析器) 自动配置了静态资源文件夹.静态首页.fa ...
- 【SpringBoot】SpringBoot与SpringMVC自动配置(五)
本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...
- 9 Web开发——springmvc自动配置原理
官方文档目录: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-sp ...
- springboot mvc自动配置(三)初始化mvc的组件
所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...
- 关于SpringBoot的自动配置和启动过程
一.简介 Spring Boot简化了Spring应用的开发,采用约定大于配置的思想,去繁从简,很方便就能构建一个独立的.产品级别的应用. 1.传统J2EE开发的缺点 开发笨重.配置繁多复杂.开发效率 ...
- 4_2.springboot2.x配置之springmvc自动配置
1.Spring MVC auto-configuration 查看官方文档: Spring Boot为Spring MVC提供了自动配置,适用于大多数应用程序. 自动配置在Spring的默认值之上添 ...
- springboot08(springmvc自动配置原理)
MVC WebMvcAutoConfiguration.java @ConditionalOnMissingBean(name = "viewResolver", value = ...
- Springboot MVC 自动配置
Springboot MVC 自动配置 官方文档阅读 https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#w ...
随机推荐
- 题目分享X
题意:一张票有n位数,如果这张票的前一半数字的和等于后一半数字的和(n一定是偶数),就称这张票为快乐票.有些数被擦除了,标记为’?’(’?‘的个数也是偶数),现在Monocarp 和 Bicarp 进 ...
- SQLite使用(二)
sqlite3_exec虽然好用,但是一般不推荐直接使用. 常用的一组操作是: 关于sqlite3_exec和sqlite3_prepare_v2的使用场景,建议如下: 一个小DEMO: #inclu ...
- K - Leapin' Lizards HDU - 2732 网络流
题目链接:https://vjudge.net/contest/299467#problem/K 这个题目从数据范围来看可以发现是网络流,怎么建图呢?这个其实不是特别难,主要是读题难. 这个建图就是把 ...
- idea撤销快捷键
Ctrl+z:撤销. Ctrl+shift+z:取消撤销.
- 微软原文翻译:适用于.Net Core的WPF数据绑定概述
原文链接,大部分是机器翻译,仅做了小部分修改.英.中文对照,看不懂的看英文. Data binding overview in WPF 2019/09/19 Data binding in Windo ...
- pycharm中的TODO注释用法
pycharm 中可以在# 后面加TODO提示自己后续的开发动作. 点击pycharm又下角的小标签,会弹出一个列表,选择TODO选项. 进入TODO选项,可以看见所以设置的TODO,选择一个TODO ...
- vue-cli中浏览器图标的配置
在VUE全家桶项目里面,这里给大家提供了2种方案,进行浏览器图标的配置. a):先把图片准备好,放在static文件夹下,再找到根目录下的index.html文件,并打开,在HTML文档的<he ...
- [hdu4763]next数组的应用
http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意:给一个字符串,判断是否可以写成ABACA,B.C表示长度大于等于0的字符串. 方法:ans = ...
- shell 光标处理快捷键
Ctrl+左右键 单词之间跳转Ctrl+a跳到本行的行首, Ctrl+e则跳到页尾. Ctrl+u删除当前光标前面的文字 ctrl+k-删除当前光标后面的文字 Ctrl+w和Alt+d-对于当前的单词 ...
- Linux中的vi编辑器使用
总是忘记,我就谢谢 touch XXX文件名 vi XXX文件名 敲击 i 进入编辑模式 敲击ESC 退出编辑模式 退出编辑模式后 输入:wq!保存并退出 输入:q!不保存退出 查看文件:cat XX ...