Springboot learning ~ 7: SpringMVC automatic configuration

Spring MVC auto-configuration

Spring Boot automatically configured the SpringMVC

The following is the default configuration for SpringMVC of SpringBoot: == (WebMvcAutoConfiguration) ==

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • Automatically configured ViewResolver (view resolver: Return worth to view objects (View) method according to decide how to render the view object (forward and redirect)??)
    • ContentNegotiatingViewResolver: all combinations ViewResolver;
    • == How Customization: We can add your own parser to view a container; a combination that will automatically come in; ==
  • Support for serving static resources, including support for WebJars (see below). Static resource folder path, webjars

  • Static index.htmlSupport. Static Home Access

  • Custom Favicon support (see below). favicon.ico

  • Automatic registration of of Converter, GenericConverter, FormatterBeans.

    • Converter: converter; public String hello (User user): type conversion with Converter
    • Formatter Formatter; 2017.12.17 === Date;
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的规则
public Formatter<Date> dateFormatter() {
    return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件
}

== add their own formatter converter, we just need to put the container ==

  • Support for HttpMessageConverters (see below).

    • HttpMessageConverter: SpringMVC for converting the Http request and response; User --- Json;

    • HttpMessageConverters It is determined from the container; HttpMessageConverter types of accessories;

      == HttpMessageConverter to add your own container, just to own components registered vessel (@ Bean, @ Component) ==

  • Registration of the Automatic MessageCodesResolver(See below). Error code generation rules defined

  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).

    == we can configure a ConfigurableWebBindingInitializer to replace the default; (added to the vessel) ==

    初始化WebDataBinder;
    请求数据=====JavaBean;

org.springframework.boot.autoconfigure.web: web of all automatic scene;

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.

Extended SpringMVC

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

== Configuration write a class (@Configuration), it is WebMvcConfigurerAdapter type; can not be marked EnableWebMvc == @ ;

Not only retains all the automatic configuration, you can also use our expanded configuration;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }
}

Principle:
. 1), is WebMvcAutoConfiguration SpringMVC autoconfiguration class
2), while doing other autoconfiguration imports; @Import ( EnableWebMvcConfiguration .class)

    @Configuration
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
      private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

     //从容器中获取所有的WebMvcConfigurer
      @Autowired(required = false)
      public void setConfigurers(List<WebMvcConfigurer> configurers) {
          if (!CollectionUtils.isEmpty(configurers)) {
              this.configurers.addWebMvcConfigurers(configurers);
                //一个参考实现;将所有的WebMvcConfigurer相关配置都来一起调用;  
                @Override
             // public void addViewControllers(ViewControllerRegistry registry) {
              //    for (WebMvcConfigurer delegate : this.delegates) {
               //       delegate.addViewControllers(registry);
               //   }
              }
          }
    }

3), the container will act with all WebMvcConfigurer;
4), our configuration is also called the class;
Effect: SpringMVC automatic configuration and function will our extended configuration;

Take full control SpringMVC

SpringBoot SpringMVC for automatic configuration is not required, and all are our own configuration; all SpringMVC automatic configuration are ineffective

We need to add to the configuration @EnableWebMvc class;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }
}

principle:

Why @EnableWebMvc automatically configured on the failure;

1) the core @EnableWebMvc

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

2)、

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
        WebMvcConfigurerAdapter.class })
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
        ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4), @ EnableWebMvc WebMvcConfigurationSupport assembly will come introduced;
. 5), only introducing WebMvcConfigurationSupport SpringMVC basic function;

Guess you like

Origin www.cnblogs.com/wbyixx/p/11875140.html