Day30——全面接管SpringMVC以及如何修改SpringBoot的默认配置

一. 回顾

前面讲了Day29——SpringMVC自动配置原理,从自动配置原理总结出如何修改SpringBoot的默认配置

二. 扩展SpringMVC

仅靠SpringBoot的自动配置有时可能不够用。我们需要自己定义配置。比如以前在springmvc.xml中配置视图映射。

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

SpringBoot官方文档中写出了扩展的方法,如下:
在这里插入图片描述
总结:编写一个配置类(@Configuration)是WebMvcConfigurerAdapter,但不能标注@EnablewebMvc

2.1 自己写一个配置类(扩展SpringMVC的功能)

特点:既保留了默认的自动配置,也能用我们扩展的配置。

最最关键的是实现WebMvcConfigurer接口

package com.atguigu.springboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//使用WebMvcConfigurerAdapter来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig2 implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        super.addViewControllers(registry);
        registry.addViewController("/atguigu2").setViewName("success");
    }
}


2.2 扩展SpringMVC的原理

  1. WebMvcAutoConfiguration是SpringMVC的自动配置类
  2. 打开WebMvcAutoConfiguration可以看到有一个WebMvcAutoConfigurationAdapter,它也实现了WebMvcConfigurer,如下:
    在这里插入图片描述
  3. 在做其他自动配置时会导入@Import(EnableWebMvcConfiguration.class)。在WebMvcAutoConfigurationAdapter上面有一个@Import(EnableWebMvcConfiguration.class),点进去看看导入的这个类是什么内容。如下:
@Configuration(proxyBeanMethods = false)
	public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration 
	                                              implements ResourceLoaderAware {

  1. 再看看它的父类DelegatingWebMvcConfiguration是有什么内容 ,如下:
@Autowired(required = false)
	public void setConfigurers(List<WebMvcConfigurer> configurers) {
		if (!CollectionUtils.isEmpty(configurers)) {
			this.configurers.addWebMvcConfigurers(configurers);
		}
	}

方法上有自动装配@Autowired,自动装配一旦标注在方法上,方法的形参就要从容器中获取。
该方法的作用是从容器中获取所有的WebMvcConfigurer

  1. 往下拉,查看到有很多Adapter中都有的配置方法,如我们上面使用的addViewControllers()方法,如下:
@Override
	protected void addViewControllers(ViewControllerRegistry registry) {
		this.configurers.addViewControllers(registry);
	}
  1. 点进去看 addViewControllers(registry),如下:
@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		for (WebMvcConfigurer delegate : this.delegates) {
			delegate.addViewControllers(registry);
		}
	}

这是把容器中所有的WebMvcConfigurer拿来,把它们(WebMvcConfigurer)的addViewControllers()方法都调一遍。

  1. 由此可以,上面的setConfigurers()方法,是把所有的WebMvcConfigurer的相关配置都来一起调用

  2. 由此观之,容器中所有的WebMvcConfigurer都会一起起作用

  3. 我们的配置类也会被调用

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

三. 全面接管SpringMVC

SpringBoot对SpringMVC的自动配置不要了,所有都是我们自己配。 我们需要在配置类添加@EnableWebMvc即可。

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

  1. 点击@EnableWebMvc看看里面有什么,如下:
    EnableWebMvc的核心:
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

它会导入DelegatingWebMvcConfiguration

  1. 点击DelegatingWebMvcConfiguration看看里面,如下:
    在这里插入图片描述
    这就是刚刚分析自己配置视图映射的原理 的 那个方法 的 父类 (即DelegatingWebMvcConfiguration) 的 方法

  2. DelegatingWebMvcConfiguration 继承了 WebMvcConfigurationSupport

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

  1. WebMvcAutoConfiguration中,它加的这些注解有如下:
    在这里插入图片描述

  2. @EnableWebMvc将将WebMvcConfigurationSupport组件导入进来

  3. 导入的WebMvcConfigurationSupport是SpringMVC最基本的功能

四. 修改默认配置的模式

  1. SpringBoot在自动配置很多组件的时候,先看有没有用户自己配置的(@Bean,@Component),如果有就用用户配置的;如果没有才自动配置。如果有些组件可以有多个(如ViewResolver),它会将用户配置的和自己默认的组合起来。

  2. 在SpringBoot中会遇到非常多的xxxConfigurer帮助我们进行扩展配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值