Spring Boot官方文档描述

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMappingRequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.


在实际开发中,仅靠SpringBoot的这点自动配置是不够用的。比如,以前有SpringMVC配置文件的时候:

 1. 原来的SpringMVC配置方式:

  • <mvc:view-controller path="/hello" view-name="success"/><!-- 配置文件设置页面跳转,发一个请求到页面,就没必要写空方法了,配一下即可 -->
    <mvc:interceptors>
    <mvc:interceptor><!-- SpringMvc 拦截器 -->
    <mvc:mapping path="/hello"/>
    <bean class=""></bean>
    </mvc:interceptor>
    </mvc:interceptors>

2. 现在的SpringBoot方式实现:

  add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc

   编写一个配置类(@Configuration),是WebMvcConfigurer(SpringBoot2.1.0))【WebMvcConfigurerAdapter(SpringBoot1.5.10)】类型;不能标注@EnableWebMvc

   既保留了所有的自动配置,也能用我们扩展的配置;

  • @Configuration
    public class MyConfig implements WebMvcConfigurer {
    /**
    * 浏览器发送addViewTest请求,来到success页面
    * 发请求到页面,就没有必要在Controller里写空方法了,直接来做视图映射
    */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/addViewTest").setViewName("success");
    } }

原理 


  • public class WebMvcAutoConfiguration {
    
        @Configuration
    @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
    @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware { } @Configuration
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration { }
    }
  • @Configuration
    public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); public DelegatingWebMvcConfiguration() {
    } /*从容器中注入所有的WebMvcConfigurer*/
    @Autowired(
    required = false
    )
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
    if (!CollectionUtils.isEmpty(configurers)) {
    this.configurers.addWebMvcConfigurers(configurers);
    } }
    /*格式化*/
    protected void addFormatters(FormatterRegistry registry) {
    this.configurers.addFormatters(registry);
    }
    /*拦截器*/
    protected void addInterceptors(InterceptorRegistry registry) {
    this.configurers.addInterceptors(registry);
    }
    /*资源文件*/
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    this.configurers.addResourceHandlers(registry);
    }
    /*视图映射*/
    protected void addViewControllers(ViewControllerRegistry registry) {
    this.configurers.addViewControllers(registry);
    }
    protected void configureViewResolvers(ViewResolverRegistry registry) {
    this.configurers.configureViewResolvers(registry);
    }
    /*参数解析*/
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    this.configurers.addArgumentResolvers(argumentResolvers);
    } ......
    }

    真正调用的实现类

  • class WebMvcConfigurerComposite implements WebMvcConfigurer {
    private final List<WebMvcConfigurer> delegates = new ArrayList();   //在上一个类中的依赖注入方法中调用的正是此方法,将所有的容器中的配资类加载到本地成员变量中
    public void addWebMvcConfigurers(List<WebMvcConfigurer> configurers) {
    if (!CollectionUtils.isEmpty(configurers)) {
    this.delegates.addAll(configurers);
    }
    }
      /**
       * 以下的每一个方法都是循环遍历容器中实现了WebMvcConfigurer接口的所有的bean,并调用相应的处理方法
        */
    public void configurePathMatch(PathMatchConfigurer configurer) {
    Iterator var2 = this.delegates.iterator(); while(var2.hasNext()) {
    WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
    delegate.configurePathMatch(configurer);
    } }
    //遍历所有的WebConfiguration类 并调用视图解析方法
    public void addViewControllers(ViewControllerRegistry registry) {
    Iterator var2 = this.delegates.iterator(); while(var2.hasNext()) {
    WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
    delegate.addViewControllers(registry);
    } }
       public void addFormatters(FormatterRegistry registry) {...}
    
       ..........
     }

   3)、容器中所有的WebMvcConfigurer都会一起起作用;

   4)、所以我们的配置类MyConfig也会被调用()

    效果:SpringMVC的自动配置 和 我们的扩展配置 都会起作用;

3、全面接管SpringMVC


SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

我们需要在配置类中添加@EnableWebMvc即可,(除非功能简单,你想节省内存空间,要不然不建议全面接管)

  •   

    //使用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");
    }
    }

原理:

为什么@EnableWebMvc自动配置就失效了;

1)@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组件导入进来;

5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;


 5、如何修改SpringBoot的默认配置模式:

​   1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;

     如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;

​   2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

​   3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

10 SpringBoot全面接管SpringMVC的更多相关文章

  1. SpringBoot接管SpringMvc

    SpringBoot接管SpringMvc Spring Web MVC framework(通常简称为“Spring MVC”)是一个丰富的“model 视图控制器”web framework. S ...

  2. 【串线篇】spring boot全面接管springMvc

    一.Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoC ...

  3. SpringBoot中对SpringMVC的自动配置

    https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...

  4. Springboot学习:SpringMVC自动配置

    Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...

  5. SpringBoot:扩展SpringMVC、定制首页、国际化

    目录 扩展使用SpringMVC 如何扩展SpringMVC 为何这么做会生效(原理) 全面接管SpringMVC 首页实现 页面国际化 SpringBoot扩展使用SpringMVC.使用模板引擎定 ...

  6. SpringBoot | 4.1 SpringMVC的自动配置

    目录 前言 1. SpringMVC框架的设计与流程 1.1 SpringMVC框架的示意图 1.2 SpringMVC的组件流程 2. *自动配置的源码分析 2.1 导入Web场景启动器 2.2 找 ...

  7. 10.spring-boot基于角色的权限管理页面实现

    10.spring-boot基于角色的权限管理页面实现

  8. 58、springmvc-定制与接管SpringMVC

    58.springmvc-定制与接管SpringMVC 定制SpringMVC: 1).@EnableWebMvc:开启SpringMVC定制配置功能: <mvc:annotation-driv ...

  9. 0011SpringBoot的@EnableWebMvc全面接管SpringMVC的自动配置(源码)

    所谓的@EnableWebMvc全面接管SpringMVC的自动配置,是指@EnableWebMvc注解会使SpringMVC的自动配置失效,原理如下: 1.查看@EnableWebMvc的源码,如下 ...

随机推荐

  1. 小项目分析之C++ 实现模拟银行排队

      一.问题定义与分析 问题定义 •要解决的问题——银行一天之内的: 1.总客户数 2.客户总逗留时间 3.客户平均逗留时间 问题分析 •新来的人找个短的队伍,站在队尾开始排队 •排在队头的人可以办理 ...

  2. 在 Linux 虚拟机中手动安装或升级 VMware Tools

    对于 Linux 虚拟机,您可以使用命令行工具手动安装或升级 VMware Tools. 本次Linux 虚拟机为CentOS6.5 先决条件开启虚拟机.确认客户机操作系统正在运行.由于 VMware ...

  3. 关于utf8mb4的学习了解笔记

    占位下班写 据说可以存储emoji ..妈蛋今天大神又秀我一脸 大概意思是,我们整个后端数据库,最近都升级了编码格式.从以前久的utf-8整个升级到了utf8mb4的格式 该格式支持emoji表情. ...

  4. BZOJ2199[Usaco2011 Jan]奶牛议会——2-SAT+tarjan缩点

    题目描述 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 <= M ...

  5. C# 枚举值 (二) 多属性 操作

    很多时候,我们的枚举值可能需要中英文, 那么可以使用下面的方法: 下面这个类,包含2部分. 1 BaseDescriptionAttribute特性的重载 2 枚举的操作类 EnumOperate n ...

  6. SharePoint 2013 Newsfeed 没有出现的解决方法

    按照这个guide配置mysite: http://technet.microsoft.com/en-us/library/ee624362(v=office.15).aspx 但是newsfeed页 ...

  7. 【转】Linux root修改密码失败

    问题: 当使用root修改密码时,报错passwd:Authentication token manipulation error 解决: 1.查看是否权限问题, /etc/passwd /etc/s ...

  8. CF528D Fuzzy Search 【NTT】

    题目链接 CF528D 题解 可以预处理出\(S\)每个位置能匹配哪些字符 对每种字符 构造两个序列 如果\(S[i]\)可以匹配该字符,则该位置为\(0\),否则为\(1\) 如果\(T[i]\)可 ...

  9. K8s核心概念详解

    kubernetes(通常简称为K8S),是一个用于管理在容器中运行的应用的容器编排工具. Kubernetes不仅有你所需要的用来支持复杂容器应用的所有东西,它还是市面上最方便开发和运维的框架. K ...

  10. 【bzoj2229】 Zjoi2011—最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=2229 (题目链接) 题意 给出一张无向图,$q$组询问,每次询问最小割不大于$c$的点对数量. So ...