1. 程式人生 > >【SpringBoot學習之路】12.SpringMVC自動配置詳解

【SpringBoot學習之路】12.SpringMVC自動配置詳解

轉載宣告:商業轉載請聯絡作者獲得授權,非商業轉載請註明出處.原文來自 © 呆萌鍾【SpringBoot學習之路】12.SpringMVC自動配置詳解 

SpringMVC自動配置

Spring MVC auto-configuration

Spring Boot 自動配置好了SpringMVC 以下是SpringBoot對SpringMVC的預設配置:(WebMvcAutoConfiguration)

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    • 自動配置了ViewResolver(檢視解析器:根據方法的返回值得到檢視物件(View),檢視物件決定如何渲染(轉發?重定向?))
    • ContentNegotiatingViewResolver:組合所有的檢視解析器的;
    • 如何定製:我們可以自己給容器中新增一個檢視解析器;自動的將其組合進來;
  • Support for serving static resources, including support for WebJars (see below).靜態資原始檔夾路徑,webjars
  • Static index.html support. 靜態首頁訪問
  • Custom Favicon support (see below). favicon.ico
  • 自動註冊了 of Converter , GenericConverter , Formatter beans.
    • Converter:轉換器; public String hello(User user):型別轉換使用Converter
    • Formatter 格式化器; 2017.12.17===Date;
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date‐format")//在檔案中配置日期格式化的規則
public Formatter<Date> dateFormatter() {
    return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化元件
}

自己新增的格式化器轉換器,我們只需要放在容器中即可

  • Support for HttpMessageConverters (see below).
    • HttpMessageConverter:SpringMVC用來轉換Http請求和響應的;User---Json;
    • HttpMessageConverters 是從容器中確定;獲取所有的HttpMessageConverter;
    • 自己給容器中新增HttpMessageConverter,只需要將自己的元件註冊容器中 (@Bean,@Component)
初始化WebDataBinder;
請求資料=====JavaBean;

org.springframework.boot.autoconfigure.web:web的所有自動場景;

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter , but without @EnableWebMvc . If you wish to provide custom instances of RequestMappingHandlerMapping , RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components. If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc .

擴充套件SpringMVC

<mvc:view-controller path="/hello" view-name="success"/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hello"/>
            <bean></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    <mvc:default-servlet-handler/>

編寫一個配置類(@Configuration),是WebMvcConfigurerAdapter型別;不能標註@EnableWebMvc;

既保留了所有的自動配置,也能用我們擴充套件的配置;

//使用WebMvcConfigurerAdapter可以來擴充套件SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        super.addViewControllers(registry);
        //瀏覽器傳送 /damienzhong 請求來到 success
        registry.addViewController("damienzhong").setViewName("success");
    }
}

原理

  • WebMvcAutoConfiguration是SpringMVC的自動配置類
  • 在做其他自動配置時會匯入;@Import(EnableWebMvcConfiguration.class)
  • 容器中所有的WebMvcConfigurer都會一起起作用
  • 我們的配置類也會被呼叫

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

全面接管SpringMVC

SpringBoot對SpringMVC的自動配置不需要了,所有都是我們自己配置;所有的SpringMVC的自動配置都失效了 我們需要在配置類中新增@EnableWebMvc即可

//使用WebMvcConfigurerAdapter可以來擴充套件SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        super.addViewControllers(registry);
        //瀏覽器傳送 /damienzhong 請求來到 success
        registry.addViewController("damienzhong").setViewName("success");
    }
}

原理

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

  • @EnableWebMvc的核心
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
@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 {
  • @EnableWebMvc將WebMvcConfigurationSupport元件匯入進來;
  • 匯入的WebMvcConfigurationSupport只是SpringMVC最基本的功能

如何修改SpringBoot的預設配置

模式

  • SpringBoot在自動配置很多元件的時候,先看容器中有沒有使用者自己配置的(@Bean、@Component)如果有就用使用者配置的,如果沒有,才自動配置;如果有些元件可以有多個(ViewResolver)將使用者配置的和自己預設的組合起來;
  • 在SpringBoot中會有非常多的xxxConfigurer幫助我們進行擴充套件配置
  • 在SpringBoot中會有很多的xxxCustomizer幫助我們進行定製配置