12、框架-SpringBoot-拓展与接管SpringMVC

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40871734/article/details/100030580

SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(如果用户自己配置@bean),如果有就用用户配置的,如果没有就用自动配置的;如果有些组件可以存在多个,比如视图解析器,就将用户配置的和自己默认的组合起来

扩展使用SpringMVC

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 RequestMappingHandlerMapping, RequestMappingHandlerAdapter, 
or ExceptionHandlerExceptionResolver, 
you can declare a WebMvcRegistrationsAdapter instance to provide such components.

我们要做的就是编写一个@Configuration注解类,并且类型要为WebMvcConfigurer,还不能标注@EnableWebMvc注解;去自己写一个;

新建一个包叫config,写一个类MyMvcConfig;

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

//应为类型要求为WebMvcConfigurer,所以我们实现其接口
//可以使用自定义类扩展MVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送/kuang , 就会跳转到success页面;
        registry.addViewController("/kuang").setViewName("success");
    }
}

浏览器访问
在这里插入图片描述
确实也跳转过来了,所以要扩展SpringMVC,官方就推荐这么使用,既保SpringBoot留所有的自动配置,也能用我们扩展的配置!
分析原理:

  1. WebMvcAutoConfiguration 是 SpringMVC的自动配置类,里面有一个类
  2. WebMvcAutoConfigurationAdapter
    这个类上有一个注解,在做其他自动配置时会导入:@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
  3. 点进EnableWebMvcConfiguration这个类看一下,它继承了一个父类:DelegatingWebMvcConfiguration,这个父类中有这样一段代码
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

   //从容器中获取所有的webmvcConfigurer
   @Autowired(required = false)
   public void setConfigurers(List<WebMvcConfigurer> configurers) {
       if (!CollectionUtils.isEmpty(configurers)) {
           this.configurers.addWebMvcConfigurers(configurers);
       }

   }

在这个类中去寻找一个刚才设置的viewController当做参考,发现它调用了一个

this.configurers.addViewControllers(registry);

点进去看一下

public void addViewControllers(ViewControllerRegistry registry) {
        Iterator var2 = this.delegates.iterator();

        while(var2.hasNext()) {
            //将所有的WebMvcConfigurer相关配置来一起调用!包括我们自己配置的和Spring给我们配置的
            WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
            delegate.addViewControllers(registry);
        }

    }
  1. 得出结论:所有的WebMvcConfiguration都会被作用,不止Spring的配置类,自己的配置类也会被调用;

全面接管SpringMVC

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

全面接管即:SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己去配置!只需在我们的配置类中要加一个@EnableWebMvc.

我们看下如果我们全面接管了SpringMVC了,我们之前SpringBoot给我们配置的静态资源映射一定会无效,我们可以去测试一下;

不加注解之前,访问首页
在这里插入图片描述
给配置类加上注解:@EnableWebMvc
在这里插入图片描述
我们发现所有的SpringMVC自动配置都失效了!回归到了最初的样子;

当然,我们开发中,不推荐使用全面接管SpringMVC

思考问题?为什么加了一个注解,自动配置就失效了!我们看下源码:

1.这里发现它是导入了一个类,我们可以继续进去看

@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

2.它继承了一个父类

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
}

3.我们来回顾一下Webmvc自动配置类

@Configuration
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//这个注解的意思就是:容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
}
  1. 总结一句话:@EnableWebMvc将WebMvcConfigurationSupport组件导入进来了;而导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能!

在SpringBoot中会有非常多的Configurer帮助我们进行扩展配置,只要看见了这个,我们就应该多留心注意

猜你喜欢

转载自blog.csdn.net/qq_40871734/article/details/100030580
今日推荐