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. Java8给出了新的特性,使得接口方法可以拥有默认实现。所以你现在可以直接实现WebMvcConfigurer而不用像以前那样通过继承它的实现类来达到目的。
      2. 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中。
      3. 总结
        
        implements WebMvcConfigurer : 不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        @EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        extends WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        extends DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
        只要使用@EnableWebMvc(隐式使用WebMvcConfigurationSupport)或显示使用WebMvcConfigurationSupport
        
        就会屏蔽springboot的@EnableAutoConfiguration中的设置
         
        @EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法
        @EnableWebMvc+extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
        extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
        extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式依旧使用springboot的@EnableAutoConfiguration中的设置

      4. 总结:大家在使用2.0版本的springboot的时候 使用WebMvcConfigurationSupport类配置拦截器时一定要重写addResourceHandlers来实现静态资源的映射,不要使用application.properties中添加配置来实现映射,不然资源会映射不成功导致打开页面资源一直加载不到。会出现下面这种奇怪的问题
      5. 扩展SpringMVC:编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;

        1.   

          @Configuration
          // WebMvcConfigurerAdapter过时,使用WebMvcConfigurer接口
          public class MyMvcConfig implements WebMvcConfigurer {
          @Override
          public void addViewControllers(ViewControllerRegistry registry) {
          // 浏览器发送 /cuzz 请求来到 success
          registry.addViewController("/cuzz").setViewName("success");
          }
          }
      6. 半全面接管SpringMVC:SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了,我们需要在配置类中添加@EnableWebMvc即可;

        1.   在WebMvcConfigurationSupport(@EnableWebMvc)和@EnableAutoConfiguration这两种方式都有一些默认的设定 
          而WebMvcConfigurationAdapter则是一个abstract class
        2. //使用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");
          }
          }
  2.   @EnableAutoConfiguration
    1. 导入jar下面的配置文件META-INF/spring.factories
    2. @EnableAutoConfiguration是springboot项目的启动类注解@SpringBootApplication的子元素,主要功能为自动配置
    3. @EnableAutoConfiguration实际是导入了EnableAutoConfigurationImportSelector和Registrar两个类
    4. @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 {
      }
    5. 这两个类的具体原理有些复杂,不太清除,主要内容是通过SpringFactoriesLoader.loadFactoryNames()导入jar下面的配置文件META-INF/spring.factories
      1. 在AutoConfigurationImportSelector类中可以看到通过 SpringFactoriesLoader.loadFactoryNames()
        把 spring-boot-autoconfigure.jar/META-INF/spring.factories中每一个xxxAutoConfiguration文件都加载到容器中,spring.factories文件里每一个xxxAutoConfiguration文件一般都会有下面的条件注解: @ConditionalOnClass : classpath中存在该类时起效
        @ConditionalOnMissingClass : classpath中不存在该类时起效
        @ConditionalOnBean : DI容器中存在该类型Bean时起效
        @ConditionalOnMissingBean : DI容器中不存在该类型Bean时起效
        @ConditionalOnSingleCandidate : DI容器中该类型Bean只有一个或@Primary的只有一个时起效
      2. SpringFactoriesLoader属于Spring框架私有的一种扩展方案(类似于Java的SPI方案java.util.ServiceLoader),其主要功能就是从指定的配置文件META-INF/spring-factories加载配置,spring-factories是一个典型的java properties文件,只不过Key和Value都是Java类型的完整类名,比如:
    6. 配置文件中的内容如下
      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,\
    7.   其中有WebMvcAutoConfiguration,WebMvcAutoConfiguration源码如下
      1. @Configuration
        @ConditionalOnWebApplication(
        type = Type.SERVLET
        )
        @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
        @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
        @AutoConfigureOrder(-)
        @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
        public class WebMvcAutoConfiguration {
        ...猜测,Spring boot 在此处加载 application.properties 中关于MVC的默认配置;如果@Conditional返回false,则该配置类不执行
        }
      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)的更多相关文章

  1. @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别

    @EnableWebMvc是什么 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration. @Retention(RetentionPolicy ...

  2. (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...

  3. spring boot(2)-@SpringBootApplication详解

    pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  4. Spring boot 梳理 - 代码结构(Main类的位置)

    Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootAppli ...

  5. Spring boot 梳理 - @Conditional

    @Conditional(TestCondition.class) 这句代码可以标注在类上面,表示该类下面的所有@Bean都会启用配置,也可以标注在方法上面,只是对该方法启用配置. spring框架还 ...

  6. Spring boot 梳理 - SpringApplication

    简单启动方式 public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, a ...

  7. Spring boot 梳理 - WebMvcConfigurer接口 使用案例

    转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,Vi ...

  8. Spring boot 梳理 - 模版引擎 -freemarker

    开发环境中关闭缓存 spring: thymeleaf: cache: false freemarker: cache: false Spring boot 集成 freemarker <dep ...

  9. Spring boot 梳理 - Spring boot 与 JSP

    若使用Spring boot 开发web应用中使用jsp,需要打包成war,并部署到非嵌入式servlet容器中运行,在嵌入式servlet中无法运行,且需要匹配非嵌入式servlet版本与Sprin ...

随机推荐

  1. Java反射使用总结

    最近公司招了几名刚毕业的大学生,在给他们培训的过程中,讲到反射,他们有些人听不懂,对反射的概念云里雾里的,不知道反射有什么用. 因此就有了本文的诞生. 反射是java提供的一个重要功能,可以在运行时检 ...

  2. unity_实用小技巧(const)

    const:声明某个常量字段或常量局部变量. 注意:常量字段和常量局部变量不是变量并且不能修改 利用const管理游戏标签 例如: //管理所有标签    public const string Pl ...

  3. Nginx总结(四)基于域名的虚拟主机配置

    前面讲了如何安装配置Nginx,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要说的 ...

  4. JavaWeb实现增删查改(图书信息管理)——之查询

     关于此次CRUD所需要的jar包,本人把文件放在了百度网盘,需要的自行去下载: 链接:https://pan.baidu.com/s/1Pqe88u6aPaeVjjOq1YFQ-w  提取码:pim ...

  5. 传入值参数&传入引用参数的区别

    传值&传引用 1.传值 是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值 2.传地址 是传值的一种特殊方式,只是他传递的是地址 那么传地址以后,实参和行参都指向同一个对象 3.传引用 ...

  6. 牛客Wannafly挑战赛23 B.游戏

    游戏 题目描述 小N和小O在玩游戏.他们面前放了n堆石子,第i堆石子一开始有ci颗石头.他们轮流从某堆石子中取石子,不能不取.最后无法操作的人就输了这个游戏.但他们觉得这样玩太无聊了,更新了一下规则. ...

  7. 杭电多校第十场 hdu6434 Count 欧拉函数打表 快速打表模板

    Problem I. Count Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. Codeforces Round #479 (Div. 3) B. Two-gram

    原题代码:http://codeforces.com/contest/977/problem/B 题解:有n个字符组成的字符串,输出出现次数两个字符组合.例如第二组样例ZZ出现了两次. 方法:比较无脑 ...

  9. HDU2276 Kiki & Little Kiki 2 矩阵快速幂

    Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  10. poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

    题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...