SpringBoot:SpringMVC的自动配置原理

1.以下是SpringBoot官方文档中关于SpringMVC的自动配置的内容


Spring MVC Auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

The auto-configuration adds the following features on top of Spring’s defaults:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

1.自动配置了ViewResolver视图解析器,根据方法的返回值得到视图对象(view),试图对象决定如何渲染(转发,重定向)。

2.ContentNegotiatingViewResolver:组合所有的视图解析器,可以理解为集成。

3.如何定制:我们可以自己给容器中添加一个视图解析器;自动地将其组合来。

静态资源文件夹路径,webjars等等。

  • Automatic registration of ConverterGenericConverter, and Formatter beans.

1.Converter:转换器;数据类型的转换器,主要是字符串和数字的转换。

2.Formatter:格式化器,主要是日期Date类型转换,需要在xml文件中配置相关转换器。

3.自定义格式化器,只需要添加到容器中即可。

1.转换消息格式,用来转换Http请求和响应的;如User->Json。

2.HttpMessageConverters是从容器中获取所有的HTTPMessageConverter类。

3.自定义消息转换器,只需要添加到容器即可。

定义错误代码生成规则的。

我们也可以配置一个ConfigurableWebBindingInitializer来替换默认的;添加到容器中。

它的作用是初始化WebdataBinder数据绑定器的,它的作用是将浏览器的请求数据绑定到javabean当中,它还会用到之前的各个组件。

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMappingRequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.


2.如何修改SpringBoot的默认配置

2.1 模式

1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean,@Component等待)如果有就用用户自己配置的,如果没有,才自动配置。如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值