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
andBeanNameViewResolver
beans.
1.自动配置了ViewResolver视图解析器,根据方法的返回值得到视图对象(view),试图对象决定如何渲染(转发,重定向)。
2.ContentNegotiatingViewResolver:组合所有的视图解析器,可以理解为集成。
3.如何定制:我们可以自己给容器中添加一个视图解析器;自动地将其组合来。
-
Support for serving static resources, including support for WebJars (covered later in this document)).
静态资源文件夹路径,webjars等等。
-
Automatic registration of
Converter
,GenericConverter
, andFormatter
beans.
1.Converter:转换器;数据类型的转换器,主要是字符串和数字的转换。
2.Formatter:格式化器,主要是日期Date类型转换,需要在xml文件中配置相关转换器。
3.自定义格式化器,只需要添加到容器中即可。
-
Support for
HttpMessageConverters
(covered later in this document).
1.转换消息格式,用来转换Http请求和响应的;如User->Json。
2.HttpMessageConverters是从容器中获取所有的HTTPMessageConverter类。
3.自定义消息转换器,只需要添加到容器即可。
-
Automatic registration of
MessageCodesResolver
(covered later in this document).
定义错误代码生成规则的。
-
Static
index.html
support. -
Custom
Favicon
support (covered later in this document). -
Automatic use of a
ConfigurableWebBindingInitializer
bean (covered later in this document).
我们也可以配置一个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 RequestMappingHandlerMapping
, RequestMappingHandlerAdapter
, 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)将用户配置的和自己默认的组合起来。