03spring boot web springmvc自動配置原理

springboot中使用springmvc
https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/boot-features-developing-web-applications.html

springboot 爲springmvc做了哪些自動配置
29.1.1 Spring MVC Auto-configuration

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
   *自動配置了ViewResolver(視圖解析器:根據方法的返回值得的視圖對象View),視圖對象決定如何渲染(轉發還是重定向)
    *ContentNegotiatingViewResolver :組合所有的視圖解析器
    *可以自定義視圖解析器
Support for serving static resources, including support for WebJars (covered later in this document)).
* 靜態資源路徑,webjars路徑
Automatic registration of Converter, GenericConverter, and Formatter beans.
  *Converter:轉換器,將頁面傳入的參數與方法中對象轉換(類型轉換)
  *Formatter :格式化器,字符串日期轉Date;
  
Support for HttpMessageConverters (covered later in this document).
 *HttpMessageConverters :springMVC用來轉換http請求和響應:對象轉json
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.

Formatter 格式化器
在這裏插入圖片描述擴展SpringMVC
配置文件的寫法
在這裏插入圖片描述
用戶自己寫View http://127.0.0.1:8080/test 結果跳轉到success.html頁面
在這裏插入圖片描述原理
在這裏插入圖片描述 在這裏插入圖片描述
全面接管SpringMVC:
通過@EnableWebMvc配置Springboot對SpringMVC的自動配置不起作用,所有都是用戶自己配置,在配置類中的地方加入@EnableWebMvc
在這裏插入圖片描述
原理 爲什麼添加@EnableWebMvc後SpringMVC的自動配置失效
在這裏插入圖片描述在這裏插入圖片描述如何修改springboot的默認配置
模式
1.springboot在自動配置很多組件的時候,先看容器中有沒有用戶自己配製的(@Bean,@Component),如果有就用用戶的配置,如果沒有就用自動配置,還有如果有些組件可以有多個(比如:ViewResolver)將用戶配置的和自動默認的組合起來;
2.在springboot中會有非常多的xxxConfigurer幫助我們進行擴展配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章