@EnableWebMvc是什么

直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...

所以@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport

@EnableWebMvc和@EnableAutoConfiguration的关系

@EnableAutoConfiguration是springboot项目的启动类注解@SpringBootApplication的子元素,主要功能为自动配置。

@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 {
...
}

@EnableAutoConfiguration实际是导入了EnableAutoConfigurationImportSelector和Registrar两个类

@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 {
}
这两个类的具体原理有些复杂,不太清除,主要内容是通过SpringFactoriesLoader.loadFactoryNames()导入jar下面的配置文件META-INF/spring.factories
 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
配置文件中的内容如下
# 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,\
...
其中有WebMvcAutoConfiguration,WebMvcAutoConfiguration源码如下
@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 {
...
}

@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})意思是如果存在它修饰的类的bean
,则不需要再创建这个bean。
由此可得出结论:
如果有配置文件继承了DelegatingWebMvcConfiguration,
或者WebMvcConfigurationSupport,或者配置文件有@EnableWebMvc,那么 @EnableAutoConfiguration 中的
WebMvcAutoConfiguration 将不会被自动配置,而是使用WebMvcConfigurationSupport的配置。

@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter使用

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中。 总结 implements WebMvcConfigurer : 不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
@EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置

@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别的更多相关文章

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

    @EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...

  2. @EnableWebMvc WebMvcConfigurer

    Spring注解@EnableWebMvc使用坑点解析 https://blog.csdn.net/zxc123e/article/details/84636521 @EnableWebMvc,Web ...

  3. springboot 2.0 配置 spring.jackson.date-format 不生效

    展开 问题:application.properties中的如下配置不生效,返回时间戳 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss 原因分析: 拦截器 ...

  4. spring boot 学习常用网站

    springboot的特性 https://www.cnblogs.com/softidea/p/5644750.html 1.自定义banner https://www.cnblogs.com/cc ...

  5. 【springboot】之 解析@EnableWebMvc 、WebMvcConfigurationSupport和WebMvcConfigurationAdapter

    springboot默认格式化日期只需要在application文件中配置 spring.jackson.date-format= yyyy-MM-dd HH:mm:ss spring.jackson ...

  6. Spring 梳理 - JavaConfig、SPI、SCI、SpringSCI、WebApplicationInitializer、AbstractAnnotationConfigDispatcherServletInitializer、WebMvcConfigurationSupport

    总结1: SCI:Servlet容器(Tomcat)提供的初始化Servlet容器本身的接口,可替换web.xml SpringSCI:SpringServletContainerInitialize ...

  7. Spring Boot实践——SpringMVC视图解析

    一.注解说明 在spring-boot+spring mvc 的项目中,有些时候我们需要自己配置一些项目的设置,就会涉及到这三个,那么,他们之间有什么关系呢? 首先,@EnableWebMvc=Web ...

  8. SpringMVC-02

    一.SSM整合[重点] 1 SSM整合配置 问题导入 请描述"SSM整合流程"中各个配置类的作用? 1.1 SSM整合流程 创建工程 SSM整合 Spring SpringConf ...

  9. SSM整合以及相关补充

    SSM整合以及相关补充 我们在前面已经学习了Maven基本入门,Spring,SpringMVC,MyBatis三件套 现在我们来通过一些简单的案例,将我们最常用的开发三件套整合起来,进行一次完整的项 ...

随机推荐

  1. PE知识复习之PE文件空白区添加代码

    PE知识复习之PE文件空白区添加代码 一丶简介 根据上面所讲PE知识.我们已经可以实现我们的一点手段了.比如PE的入口点位置.改为我们的入口位置.并且填写我们的代码.这个就是空白区添加代码. 我们也可 ...

  2. docker 安装LAMP环境

    LAMP:Linux.Apache.MySQL.PHP docker hub 上会有配好的LAMP环境docker,部署到本地并运行起来 sudo docker pull linode/lamp 然后 ...

  3. alibaba / zeus 安装 图解

    一.首先需要到https://github.com/alibaba/zeus下载相应的安装文件 二.解压缩导入到eclipse工程

  4. 分享几个有趣的Linux命令

    前言 最近工作比较忙,没时间写博客,这次介绍几个有趣的Linux命令. 命令:sl 当你使用这个命令时会看到一辆小火车从你的屏幕经过.亲测! 安装命令如下: yum -y install sl 执行效 ...

  5. Python爬虫之使用celery加速爬虫

      celery是一个基于分布式消息传输的异步任务队列,它专注于实时处理,同时也支持任务调度.关于celery的更多介绍及例子,笔者可以参考文章Python之celery的简介与使用.   本文将介绍 ...

  6. [日常] imap协议读取邮件

    telnet imap.sina.net 143 A01 LOGIN shihan@appdev.sinanet.com 密码 A02 list "" * //列出邮件夹 * LI ...

  7. Java开发笔记(六十)匿名内部类的优势

    前面依次介绍了简单接口和扩展接口,给出的范例都是自定义的接口代码,其实Java系统本身就自带了若干行为接口,为了更好地理解系统接口的详细用法,接下来还是从一个基础的例子出发,抽丝剥茧地逐步说明接口的几 ...

  8. 当桌面的快捷方式图标左下角出现一个X(叉)的时候应该怎么去掉

    win+r打开运行,然后复制粘贴如下命令就OK辣 cmd /k reg delete "HKEY_CLASSES_ROOT\lnkfile" /v IsShortcut /f &a ...

  9. Tomcat安装教程

    Tomcat安装教程 文档下载:https://files-cdn.cnblogs.com/files/yocichen/Tomcat安装教程.rar 注意:本教程适用Windows平台安装Tomca ...

  10. Netty学习笔记(五) 使用Netty构建静态网页服务器

    昨天在继续完善基于Netty构建的聊天室系统的过程中,发现了一个有意思的知识点,特此拿来做一个简单的静态网页服务器,好好的玩一玩Netty. 但是不管怎么说利用netty实现各种功能的流程都是类似的 ...