前言
SpringMVC定比特於一個較為松散的組合,展示給用戶的視圖(View)、控制器返回的數據模型(Model)、定比特視圖的視圖解析器(ViewResolver)和處理適配器(HandlerAdapter)等容器都是獨立的。換句話說,通過SpringMVC很容易把後臺的數據轉換為各種類型的數據,以滿足移動互聯網數據多樣化的要求。
本篇僅為簡單介紹SpringMVC的大致組件與流程,詳細過程將在後續篇章一一道來。
1. SpringMVC框架的設計與流程
流程和組件是SpringMVC的核心,SpringMVC的流程是圍繞DispatcherServlet而工作的。
1.1 SpringMVC框架的示意圖
1.2 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
:
它定義的對象在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
andBeanNameViewResolver
beans.- 內容協商視圖解析器和BeanName視圖解析器;
Support for serving static resources, including support for WebJars (covered later in this document)).
- 靜態資源(包括webjars);
Automatic registration of
Converter
,GenericConverter
, andFormatter
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 typeWebMvcConfigurer
but without@EnableWebMvc
.不用@EnableWebMvc注解。使用@Configuration+WebMvcConfigurer自定義規則;
If you want to provide custom instances of
RequestMappingHandlerMapping
,RequestMappingHandlerAdapter
, orExceptionHandlerExceptionResolver
, and still keep the Spring Boot MVC customizations, you can declare a bean of typeWebMvcRegistrations
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
-annotatedDelegatingWebMvcConfiguration
as described in the Javadoc of@EnableWebMvc
.使用@[email protected]+DelegatingWebMvcConfiguration 全面接管SpringMVC;
4. 定制SpringMVC的初始化
Spring提供WebMvcConfigurer接口;對應SpringBoot提供WebMvcAutoConfiguration接口。
4.1 WebMvcConfigurer與WebMvcAutoConfiguration的關系圖
在SpringBoot中,自定義通過配置類WebMvcAutoConfiguration
定義的,它有一個靜態的內部類WebMVCAutoConfigurationAdapter
,通過它SpringBoot就自動配置了SpringMVC的初始化。
4.2 SpringMVC可配置項
在WebMVCAutoConfigurationAdapter
類中,它會讀入Spring配置SpringMVC的屬此來初始化對應組件,這樣便能够在一定程度上實現自定義。可配置項如下:
除此之外,還可以實現WebMvcConfigurer
接口加入自己定義的方法。
最後
新人制作,如有錯誤,歡迎指出,感激不盡!
歡迎關注公眾號,會分享一些更日常的東西!
如需轉載,請標注出處!
SpringBoot | 4.1 SpringMVC的自動配置的更多相關文章
- SpringBoot中對SpringMVC的自動配置
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...
- java框架之SpringBoot(5)-SpringMVC的自動配置
本篇文章內容詳細可參考官方文檔第 29 節. SpringMVC介紹 SpringBoot 非常適合 Web 應用程序開發.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...
- SpringBoot中SpringMVC的自動配置以及擴展
一.問題引入 我們在SSM中使用SpringMVC的時候,需要由我們自己寫SpringMVC的配置文件,需要用到什麼就要自己配什麼,配置起來也特別的麻煩.我們使用SpringBoot的時候沒有進行配置 ...
- 7、springmvc的自動配置
1.springmvc的自動配置 文檔:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot ...
- SpringMVC的自動配置解析
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...
- SpringBoot入門(四)——自動配置
本文來自網易雲社區 SpringBoot之所以能够快速構建項目,得益於它的2個新特性,一個是起步依賴前面已經介紹過,另外一個則是自動配置.起步依賴用於降低項目依賴的複雜度,自動配置負責减少人工配置的工 ...
- 0011SpringBoot的@EnableWebMvc全面接管SpringMVC的自動配置(源碼)
所謂的@EnableWebMvc全面接管SpringMVC的自動配置,是指@EnableWebMvc注解會使SpringMVC的自動配置失效,原理如下: 1.查看@EnableWebMvc的源碼,如下 ...
- 接管SpringBoot對Activiti的數據源自動配置
SpringBoot的自動配置真的讓人又愛又恨,但還是愛更多一點. SpringBoot想要幫我們自動配置好一切,但是有時候配置的卻並不是我們需要的,甚至有時候會默默的坑我們. 我的項目是一個多數據源 ...
- springboot web項目創建及自動配置分析(thymeleaf+flyway)
@ 目錄 源碼分析 webjars thymeleaf thymeleaf語法 springmvc 啟動配置原理 集成flyway插件 springboot 創建web項目只需要引入對應的web-st ...
- 面試官:給我講講SpringBoot的依賴管理和自動配置?
1.前言 從Spring轉到SpringBoot的xdm應該都有這個感受,以前整合Spring + MyBatis + SpringMVC我們需要寫一大堆的配置文件,堪稱配置文件地獄,我們還要在pom ...
隨機推薦
- Le lié à la légèreté semblait être et donc plus simple
Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...
- MS SQL Server 數據庫分離-SQL語句
前言 今天在在清理數據庫,是MS SQL Server,其中用到分離數據庫文件.在這過程中,出現了一個小小的問題:誤將數據庫日志文件删除了,然後數據就打不開了,除了脫機,其他操作都報錯. 數據庫分離 ...
- VS2015+cordova+ionic安裝配置
VS2015已經出了正式版,想用來試一下cordova方面的開發.最近在看ionic這個框架,於是想能在VS2015裏編輯js就好了. 下面說一下蛋疼的安裝配置過程. 一.安裝VS2015及Andro ...
- 什麼時候用@Resource,什麼時候用@service
Spring中什麼時候用@Resource,什麼時候用@service當你需要定義某個類為一個bean,則在這個類的類名前一行使用@Service("XXX"),就相當於講這個類定 ...
- 用fxc.exe編譯shader文件(*.fx, *.hlsl)的設置
原文出自:http://msdn.microsoft.com/en-us/library/windows/desktop/bb509709(v=vs.85).aspx#Profiles 拿DX11 S ...
- 關於Entity Framework中的Attached報錯的完美解决方案
我們在使用Entity Framework進行CRUD時,為了提昇查詢效率,一般均會啟動NoTracking,即不追踪變化,設置代碼如下: //這是DB First模式下設置方法: aTestEnti ...
- 2016CCPC東北地區大學生程序設計競賽 1001 HDU5922
鏈接http://acm.hdu.edu.cn/showproblem.php?pid=5922 題意:最小生成樹,但邊的權值是連接兩點的最小公倍數 解法:不要真的寫最小生成樹啦,只要其他點和第一點相 ...
- 使用ASP.NET 構建 Web 應用程序快速入門-8小時的免費培訓視頻
- Scott Hanselman的中文博客[轉載] [原文發錶地址] Building Web Apps with ASP.NET Jump Start - 8 Hours of FREE Trai ...
- Merge使用
Role r = new Role(); r.setName("TEST"); r.setDescription("123"); r.setLevel(2); ...
- java_多線程4種實現方式
為了34月份回學校春招,不得不複習一下線程的四種實現方式,希望春招時能找到更好的公司,加油! 1.繼承Thread類 class MyThread extends Thread{ private in ...