Spring MVC 自动配置原理

本文导读

  • Spring Boot 自动配置好了SpringMVC,所以应用新建后,直接像以前Spring MVC一样写代码就能用
  • 本文主要讲解 Spring Boot 对 Spring mVC 底层的自动配置

官方介绍

  • 可以参考 《官方文档》,下面就是官方对 Spring Boot 关于 Spring MVC 自动配置的介绍
27.1.1 Spring MVC Auto-configuration
//Spring MVC 自动配置

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
//Spring Boot 为Spring MVC提供了自动配置,适用于大多数应用程序。

The auto-configuration adds the following features on top of Spring’s defaults:
//自动配置默认添加了如下的 Spring 特性配置

    Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    Support for serving static resources, including support for WebJars (covered later in this document)).
    Automatic registration of Converter, GenericConverter, 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 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.

Spring MVC 自动配置详解

  • 下面来对官方对于Spring MVC默认配置进行一一解释
  • 既然是MVC的自动配置,所以内容都可以在“WebMvcAutoConfiguration”类中找到
package org.springframework.boot.autoconfigure.web.servlet;

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
	public static final String DEFAULT_PREFIX = "";

	public static final String DEFAULT_SUFFIX = "";

	private static final String[] SERVLET_LOCATIONS = { "/" };
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
  • 自动配置了 ViewResolver(视图解析器),即根据方法的返回值得到视图(View)对象,视图对象决定如何渲染(转发或是重定向或是其它)
  • ContentNegotiatingViewResolver:用于组合所有的视图解析器;所以可以自己给容器中添加一个视图解析器,启动时会自动将其组合进来;
  • 自定义视图解析器:1、新建类继承 ViewResolver 类,2、然后重写ViewResolver唯一的方法,3、然后将自己的视图解析器添加到Spring容器中(可以使用@Component或者@Configuration加@Bean)
Support for serving static resources, including support for WebJars (covered later in this document)).
Automatic registration of Converter, GenericConverter, and Formatter beans.
  • 自动注册了 Converter , GenericConverter , Formatter
  • Converter:转换器,用于页面提交数据时的类型转换,如提交的字符串“18”后台接收时转为Integer类型
  • Formatter:格式化器;,用于格式化页面提交的日期数据,如“2017-12-17”接收后要转为Date类型数据
  • GenericConverter:同样转换器
Support for HttpMessageConverters (covered later in this document).
  • HttpMessageConverter:SpringMVC 用来转换Http请求和响应的,如控制器方法以Json数据格式返回一个User对象给前端页面
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).
  • 初始化WebDataBinder,可用于将页面请求数据直接封装成JavaBean;

修改 Spring Boot 默认组件

  • Spring Boot 在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component),如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(如ViewResolver),则将用户配置的和自己默认的组合起来;
  • 在 Spring Boot 中会有很多的xxxConfigurer 帮助用户进行扩展配置
  • 在 Spring Boot 中会有很多的xxxCustomizer帮助用户进行定制配置

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/81079056