SpringBoot对SpringMVC的支持

7.1.1. 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.

  • Support for serving static resources, including support for WebJars (covered later in this document)).

  • Automatic registration of ConverterGenericConverter, and Formatter beans.

  • Support for HttpMessageConverters (covered later in this document).

  • 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).

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 RequestMappingHandlerMappingRequestMappingHandlerAdapter, 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.

  https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-spring-mvc  

  SpringBoot官方文档中,SpringBoot已经提供了SpringMVC大部分工作场景所需的自动配置,可以直接使用。

  默认配置有:  

      • ViewResolver 视图解析器,根据方法返回值得到视图对象(View),视图对象决定如何渲染(转发或者重定向 )。
      • ContentNegotiatingViewResolver:组合所有的视图解析器,该类会从BeanFactory中获取所有视图解析器并且组合。如果需要自己定制视图解析器,就可以向容器中添加视图解析器,最终会被组合进框架中。
      •  静态资源文件夹路径、wabjars
      • 静态首页访问
      • 图标
      • 自动注册Converter、Formatter。转换器和格式化器。在处理请求时转换器将页面提交的内容转换成对应类型。格式化器可以将 例如2020/1/30 -->Date类型。如果需要自己添加格式化转换器,只需要将自己写好的Bean添加进容器中即可。
      •  HttpMessageConverter:消息转换器,用于转换Http请求和响应;例如返回User对象, 如果想返回json格式返回,就需要转换器 User --> Json 如果要添加自己的HttpMessageConverter,也是将自己的组件注册到容器中就行了。默认已经配好了Json和xml类型的转换器。
      • MessageCodesResolver :定义错误代码生成规则
      • ConfigurableWebBindingInitializer :初始化WebDataBinder:可以将请求数据绑定到JavaBean中。 可以自己编写一个来替换默认的组件(添加自己的Bean到容器中)
      • 在org.springframework.boot.autoconfigure.web:中有自动配置有关的内容

修改SpringBoot的默认配置

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

  2、在SpringBoot中会有非常多的xxxConfigurer进行扩展配置。

扩展SpringMVC

  编写一个配置类(@Configuration),实现WebMvcConfigurer接口进行扩展,不能标注@EnableWebMvc注解,重写接口提供的方法即可。可以既保留所有自动配置,也能用扩展的配置。

  例如添加视图映射:

 

 

   在启动时会将所有WebMvcConfiurer相关配置都调用一遍

    最终SpringMVC的自动配置和扩展配置都会起作用 

完全接管SpringMVC

  如果要完全接管SpringMVC,就不需要自动配置了,所有内容都自己配。类似于SSM开发时使用SpringMVC。 只需要在配置类上添加@EnableWebMvc即可

posted @ 2020-01-30 11:29  ELAIRS  阅读(252)  评论(0编辑  收藏  举报