SpringBoot | 4.1 SpringMVC的自動配置



前言

SpringMVC定位於一個較為松散的組合,展示給用戶的視圖(View)、控制器返回的數據模型(Model)、定位視圖的視圖解析器(ViewResolver)和處理適配器(HandlerAdapter)等容器都是獨立的。換句話說,通過SpringMVC很容易把后台的數據轉換為各種類型的數據,以滿足移動互聯網數據多樣化的要求。

本篇僅為簡單介紹SpringMVC的大致組件與流程,詳細過程將在后續篇章一一道來。


1. SpringMVC框架的設計與流程

流程和組件是SpringMVC的核心,SpringMVC的流程是圍繞DispatcherServlet而工作的。

1.1 SpringMVC框架的示意圖

SpringMVC框架的示意圖

1.2 SpringMVC的組件流程

SpringMVC流程
大致流程是:首先是定義請求分發,讓SpringMVC能夠產生HandlerMapping;其次是接收請求獲取參數;再次是處理業務邏輯獲取數據模型ModelAndView;最后是綁定視圖和數據模型。

以上組件將會在后續文章講解,這里僅做一個大概介紹。

組件名稱 組件說明
DispatcherServlet 核心組件,前端控制器;
LocalResolver 國際化解析器;
ThemeResolver 主體解析器;
HandlerMapping 處理器映射;
HandlerAdapter 處理器適配器;
HandlerExceptionResolver 處理器異常解析器;
RequestToViewNameTranslator 策略視圖名稱轉換器;
ViewResolver 視圖解析器;
FalshMapManager 不常用,FlashMap管理;

以上組件會在SpringMVC初始化時構建出來。


2. *自動配置的源碼分析

SpringMVC的自動配置流程是類似第三章了數據庫組件自動配置相關內容。

2.1 導入Web場景啟動器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2 找到DispatcherServlet的屬性文件

前面提到SpringMVC的核心是DispatcherServlet前端控制器,因此我們找到它的屬性文件DispatcherServlet.properties

DispatcherServlet的屬性文件
它定義的對象在SpringMVC開始時就初始化,並且注冊進Spring IoC容器中。此外,在這個jar包內定義了很多SpringMVC相關的組件。


3. 自動配置的官網描述

SpringBoot配置SpringMVC在SpringBoot官網已經說明了,可以參考以下翻譯。

官網地址:7.1.1. Spring MVC Auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.(SpringBoot為SpringMVC提供了自動配置,因此大多場景我們都無需自定義配置)

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

(自動化配置包括以下默認特性)

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 內容協商視圖解析器和BeanName視圖解析器;
  • Support for serving static resources, including support for WebJars (covered later in this document)).

    • 靜態資源(包括webjars);
  • Automatic registration of Converter, GenericConverter, and Formatter beans.

    • 自動注冊 Converter,GenericConverter,Formatter;
  • Support for HttpMessageConverters (covered later in this document).

    • 支持 HttpMessageConverters(后續文章有內容協商原理分析);
  • Automatic registration of MessageCodesResolver (covered later in this document).

    • 自動注冊 MessageCodesResolver (國際化用,少用,一般直接開發兩套頁面);
  • Static index.html support.

    • 靜態index.html 頁支持;
  • Custom Favicon support (covered later in this document).

    • 自定義Favicon;
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

    • 自動使用 ConfigurableWebBindingInitializer,(DataBinder負責將請求數據綁定到JavaBean上);

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.

不用@EnableWebMvc注解。使用@Configuration+WebMvcConfigurer自定義規則

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.

聲明WebMvcRegistrations改變默認底層組件

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.

使用@EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC


4. 定制SpringMVC的初始化

Spring提供WebMvcConfigurer接口;對應SpringBoot提供WebMvcAutoConfiguration接口。

4.1 WebMvcConfigurer與WebMvcAutoConfiguration的關系圖

WebMvcConfigurer與WebMvcAutoConfiguration的關系圖
在SpringBoot中,自定義通過配置類WebMvcAutoConfiguration定義的,它有一個靜態的內部類WebMVCAutoConfigurationAdapter,通過它SpringBoot就自動配置了SpringMVC的初始化。

4.2 SpringMVC可配置項

WebMVCAutoConfigurationAdapter類中,它會讀入Spring配置SpringMVC的屬此來初始化對應組件,這樣便能夠在一定程度上實現自定義。可配置項如下:

SpringMVC可配置項

除此之外,還可以實現WebMvcConfigurer接口加入自己定義的方法。



最后

新人制作,如有錯誤,歡迎指出,感激不盡!
歡迎關注公眾號,會分享一些更日常的東西!
如需轉載,請標注出處!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM