Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

  1. @EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport
    1. 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration  
      1. @Retention(RetentionPolicy.RUNTIME)
        @Target({ElementType.TYPE})
        @Documented
        @Import({DelegatingWebMvcConfiguration.class})
        public @interface EnableWebMvc {
        }

         

      2. DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport
        1. @Configuration
          public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
          ...
    2. @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter使用

      1. WebMvcConfigurationAdapter已经废弃,最好用implements WebMvcConfigurer代替

        @Configuration
        public class MyConfig implements WebMvcConfigurer {
            
        }

        如果使用继承,WebMvcConfigurationSupport,DelegatingWebMvcConfiguration,或者使用@EnableWebMvc,

        需要注意会覆盖application.properties中关于WebMvcAutoConfiguration的设置,需要在自定义配置中实现,如

        springboot2.0、spring5.0 拦截器配置WebMvcConfigurerAdapter过时使用WebMvcConfigurationSupport来代替 新坑

        示例如下

        Configuration
        @EnableWebMvc
        public class MyConfig implements WebMvcConfigurer {
        
        }
        @Configuration
        public class MyConfig extends WebMvcConfigurationSupport {
        
        }
        @Configuration
        public class MyConfig extends DelegatingWebMvcConfiguration {
        
        }
        上面代码中需要在类中实现关于WebMvcAutoConfiguration的配置,而不是在application.properties中。
      2. 总结

        implements WebMvcConfigurer : 不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        @EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        extends WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        extends DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
  2.   @EnableAutoConfiguration
    1. @EnableAutoConfiguration是springboot项目的启动类注解@SpringBootApplication的子元素,主要功能为自动配置
    2. @EnableAutoConfiguration实际是导入了EnableAutoConfigurationImportSelector和Registrar两个类
    3. @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Inherited
      @AutoConfigurationPackage
      @Import({AutoConfigurationImportSelector.class})
      public @interface EnableAutoConfiguration {
      ...
      }
      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Inherited
      @Import({Registrar.class})
      public @interface AutoConfigurationPackage {
      }

       

    4. 这两个类的具体原理有些复杂,不太清除,主要内容是通过SpringFactoriesLoader.loadFactoryNames()导入jar下面的配置文件META-INF/spring.factories
    5. 配置文件中的内容如下
      1. # Auto Configure
        org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,...
        org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\

         

    6.   其中有WebMvcAutoConfiguration,WebMvcAutoConfiguration源码如下
      1. @Configuration
        @ConditionalOnWebApplication(
            type = Type.SERVLET
        )
        @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
        @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
        @AutoConfigureOrder(-2147483638)
        @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
        public class WebMvcAutoConfiguration {
        ...
        }

         

      2. @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})意思是如果存在它修饰的类的bean
        ,则不需要再创建这个bean。
      3. 由此可得出结论:
        如果有配置文件继承了DelegatingWebMvcConfiguration,
        或者WebMvcConfigurationSupport,或者配置文件有@EnableWebMvc,那么 @EnableAutoConfiguration 中的
        WebMvcAutoConfiguration 将不会被自动配置,而是使用WebMvcConfigurationSupport的配置。
  3. @SpringBootApplication
    1. @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Inherited
      @SpringBootConfiguration
      @EnableAutoConfiguration
      @ComponentScan(
          excludeFilters = {@Filter(
          type = FilterType.CUSTOM,
          classes = {TypeExcludeFilter.class}
      ), @Filter(
          type = FilterType.CUSTOM,
          classes = {AutoConfigurationExcludeFilter.class}
      )}
      )
      public @interface SpringBootApplication {
      ...
      }

       

  4. 转:https://www.cnblogs.com/sufferingStriver/p/9026764.html

Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

上一篇:go语音基础之append函数的使用


下一篇:iOS开发从申请开发账号到APP上架的整体流程详解