天天看點

SpringBoot自學好幾天 中途開始寫筆記 SpringBoot Web開發 SpringMVC自動配置和原理 擴充 及 全面接管 20190121

1.SpringMVC自動配置

關于SpringMVC 我沒有系統學習過 隻是項目中可能會簡單使用,是以準備學完SpringBoot之後 找個相關視訊 系統學習一下SpringMVC

​​點選官方關于SpingMVC說明位址​​

Spring Boot 自動配置好了SpirngMVC

以下是SpringBoot對SpringMCV的自動配置:

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

  • 自動配置了視圖解析器(ViewResolver)
  • 視圖解析器:根據方法的傳回值得到視圖對象(view對象),視圖對象決定如何渲染(轉發?重定向?)
  • ContentNegotiatingViewResolver 組合所有視圖解析器
  • 如何定制:我們可以自己給容器添加視圖解解析器,會被自動組合進來

Support for serving static resources, including support for WebJars (covered later in this document)).

靜态資源檔案夾路徑

Static index.html support.

靜态首頁通路

Custom Favicon support (covered later in this document).

favivon.ico

Automatic registration of Converter, GenericConverter, and Formatter beans.

- Converter :類型轉換器

public String hello(Model model ) 頁面字元串 轉背景各種類型(Integer Boolean 等)

- Formatter :格式化器 ; 2018-12-12 ===》Date

自己添加的格式化轉換器 隻需要放到容器中即可

Support for HttpMessageConverters (covered later in this document).

- HttpMessageConverters :SpringMVC用來轉換HTTP請求和響應的

- HttpMessageConverters 是從容器中确定的,擷取容器中所有的HttpMessageConverters 自己給容器添加的話,隻需要将自己的元件放到容器中

Automatic registration of MessageCodesResolver (covered later in this document). 定義錯誤代碼輸出規則

Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

初始化web資料綁定器 請求資料==》JavaBean 他會牽扯到類型轉換 格式化等元件

我們可以配置ConfigurableWebBindingInitializer 替換預設的(添加到容器中即可)

​​org.springframework.boot.autoconfigure.web​​:web的所有自動配置場景

2. 如何修改SpringBoot的預設配置

模式:

1)SpringBoot 在配置很多元件的時候,先看使用者中有沒有使用者自定義元件 (@Bean @Component) 如果有就用使用者配置的。

如果有些元件可以有多個(比如:視圖解析器),将會把使用者配置的與自己的合并;

2)在SpringBoot中 會有很多xxxConfigur 幫助我們進行擴充配置

3)xxxCustomizer 幫助我們進行定制配置

3. 擴充SpringMVC
SpringBoot自學好幾天 中途開始寫筆記 SpringBoot Web開發 SpringMVC自動配置和原理 擴充 及 全面接管 20190121
<mvc:view-controller path="/hello" view-name="succ"/>
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/hello"/>
                <bean></bean>
            </mvc:interceptor>
        </mvc:interceptors>      

編寫一個配置類(@Configuration ) 繼承 WebMvcConfigurer (2.0之後)

不能有@EnableWebMvc 注解 如果标注@EnableWebMvc 就會完全掌控SpringMVC

/**
 * @author LM
 * @create 2019-01-21 21:11
 */
@Configuration

public class MyMVCConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏覽器發送 hello請求 就傳回到succ 頁面
        registry.addViewController("/hellotest").setViewName("succ");
    }
}      

原理:

1)WebMvcAutoConfiguration 是SpringMVC的自動配置類

2)在做其他自動配置時會導入, @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})

@Configuration
 public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {

------------------------------------------------------------
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

    public DelegatingWebMvcConfiguration() {
    }

    @Autowired(
        required = false
    )//從容器中擷取所有的WebMvcConfigurer
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
            //一個參考實作;将所有的WebMvcConfigurer相關配置都來一起用
        //    public void addViewControllers(ViewControllerRegistry registry) {
        //          Iterator var2 = this.delegates.iterator();
        //         while(var2.hasNext()) {
        //              WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
        //             delegate.addViewControllers(registry);
        //         }
        //      }
        }

    }      

3)容器中所有的WebMvcConfigurer 都會一起起作用

4)我們的配置類 也會被調用

效果:SpringMVC的自動配置 和 我們的擴充配置都會起作用

為什麼加了@EnableWebMvc 自動配置就失效了;

1)EnableWebMvc 核心

@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}      

2)

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {      
@Configuration
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {      

看清楚了!!! @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})

如果容器中沒有WebMvcConfigurationSupport 類才會生效 !!

EnableWebMvc 已經給我們導入了WebMvcConfigurationSupport 是以

繼續閱讀