官方文档描述(springboot2.4version),主要参考springboot文档写下这个文章
springboot:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc
spring-webmvc:https://docs.spring.io/spring-framework/docs/5.3.4/reference/html/web.html#mvc
通过官网文档学习springmvc
image.png

springboot如何自动配置springmvc

springboot自动帮我们配置好的

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:

  1. Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
  2. Support for serving static resources, including support for WebJars
  3. Automatic registration of Converter, GenericConverter, and Formatter beans.
  4. Support for HttpMessageConverters
  5. Automatic registration of MessageCodesResolver
  6. Static index.html support.
  7. Automatic use of a ConfigurableWebBindingInitializer bean

    三种修改配置文件的方法(1普通,2定制化,3全部重置)太干了

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

  9. 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.
  10. 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.

    自动配置的源码

    自动配置类:org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
    源码自己可以去看
    简单分析一下作用:

    1. @Configuration(proxyBeanMethods = false)
    2. @ConditionalOnWebApplication(type = Type.SERVLET)
    3. @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
    4. @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    5. @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
    6. @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
    7. ValidationAutoConfiguration.class })
    8. public class WebMvcAutoConfiguration

    这是类上的注解,说明了生效和不生效的条件,以及生效的一个顺序,具体的配置要看里面嵌套的配置类和注册Bean
    image.png
    主要功能点的逻辑在内部的2个静态配置类

  11. public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer

    1. 功能点就是springboot帮我们写的逻辑都在这个类
    2. 我们自己额外添加的功能也仿照这个类去写,这是官方的建议
  12. public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware
    1. 其中DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport,官方建议如果要完全接管springmvc,就使用@EnableWebMvc或者继承DelegatingWebMvcConfiguration然后自己实现 ,最原始的就继承WebMvcConfigurationSupport,然后自己重写大部分的功能
    2. WebMvcConfigurationSupport相当于是一个原始的配置类,只有基本的实现,子类DelegatingWebMvcConfiguration 实现少许功能,EnableWebMvcConfiguration 实现大部分功能

image.png