使用SpringBoot如何实现全面接管扩展SpringMVC

  介绍

本篇文章给大家分享的是有关使用SpringBoot如何实现全面接管扩展SpringMVC,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

如果想在SpringBoot中扩展一些SpringMVC的配置,例如需要配置自定义的视图解析器或拦截器等,需要怎么实现呢?
例如,自定义一个视图解析器:

@ configuration   公共类MyConfig实现WebMvcConfigurer {   @Override   公共空间addViewControllers (ViewControllerRegistry注册表){   registry.addViewController (“/index") .setViewName (“index");   }   }

我们只需要编写一个配置类去实现WebMvcConfigurer接口,并选择实现接口中的方法,不能标注@EnableWebMvc,这些WebMvcConfigurer接口中的方法就是SpringMVC所可以扩展的配置

注意:在SpringBoot1.0版本中扩展SpringMVC配置是继承WebMvcConfigurerAdapter类,但在2.0以上的版本中已经过时,官方推荐使用以上实现WebMvcConfigurer接口的方式进行扩展,因为在2.0版本中WebMvcConfigurer接口有了默认实现。

WebMvcConfigurer方法介绍:这里只列举几个比较关键的方法

公共接口WebMvcConfigurer {//定制URL匹配规则
  默认的空隙configurePathMatch (PathMatchConfigurer配置){
  }//内容协商机制
  默认的空隙configureContentNegotiation (ContentNegotiationConfigurer配置){
  }//异步任务执行器。
  默认的空隙configureAsyncSupport (AsyncSupportConfigurer配置){
  }//使用默认servlet处理静态资源
  默认的空隙configureDefaultServletHandling (DefaultServletHandlerConfigurer配置){
  }//添加格式转换器
  默认的空隙addFormatters (FormatterRegistry registry) {
  }//添加拦截器
  默认的空隙addInterceptors (InterceptorRegistry registry) {
  }//添加视图解析器
  默认的空隙addViewControllers (ViewControllerRegistry registry) {
  }
  }

扩展MVC的实现原理:

我们都知道WebMvcAutoConfiguration是SpringMVC的自动配置类,当在做其他配置导入时,导入了@ import (EnableWebMvcConfiguration.class)这样一个注解,这个注解有什么用?

@ configuration (proxyBeanMethods=false)
@ import (EnableWebMvcConfiguration.class)
@EnableConfigurationProperties ({WebMvcProperties。类,ResourceProperties。类})
@Order (0)
公共静态类WebMvcAutoConfigurationAdapter实现WebMvcConfigurer {
}

点进这个注解,发现他还是WebMvcAutoConfiguration里的一个静态内部类,但他继承了DelegatingWebMvcConfiguration

@ configuration (proxyBeanMethods=false)
公共静态类EnableWebMvcConfiguration延伸DelegatingWebMvcConfiguration实现ResourceLoaderAware {
}

再点进这个DelegatingWebMvcConfiguration类里,开头有这样一段代码,有一个configurers属性,类型是WebMvcConfigurerComposite,这个WebMvcConfigurerComposite类也实现了WebMvcConfigurer,当@ autowired标注在一个方法上说明,这个方法的参数都从容器中获取,这里是从容器中获取所有的WebMvcConfigurer,并赋值给了configurers属性

 @ configuration (proxyBeanMethods=false)
  公开课DelegatingWebMvcConfiguration延伸WebMvcConfigurationSupport {
  
  私人最终WebMvcConfigurerComposite configurers=new WebMvcConfigurerComposite ();
  
  @ autowired(要求=false)
  公共空间setConfigurers (Listconfigurers) {
  如果(! CollectionUtils.isEmpty (configurers)) {
  this.configurers.addWebMvcConfigurers (configurers);
  }
  }
  }

在这个类往下看,发现这个类的方法跟WebMvcConfigurer接口里的方法一样,以这个视图解析器举例,方法里调用了这个方法this.configurers.addViewControllers(注册中心)

 @ configuration (proxyBeanMethods=false)
  公开课DelegatingWebMvcConfiguration延伸WebMvcConfigurationSupport {
  
  私人最终WebMvcConfigurerComposite configurers=new WebMvcConfigurerComposite ();
  
  @ autowired(要求=false)
  公共空间setConfigurers (Listconfigurers) {
  如果(! CollectionUtils.isEmpty (configurers)) {
  this.configurers.addWebMvcConfigurers (configurers);
  }
  }
  
  …
  
  @Override
  保护无效addViewControllers (ViewControllerRegistry注册表){
  this.configurers.addViewControllers(注册表);
  }
  }

点进configurers.addViewControllers(注册中心),这个方法是把容器中所有的addViewControllers()都执行一遍。你们;马克祝辞因为我们自己写的配置类也注入到了容器里,所以我们的配置也会被调用,并且也被SpringBoot自动配置上,所以SpringMVC的自动配置和我们的扩展配置都会起作用& lt;/mark>;

使用SpringBoot如何实现全面接管扩展SpringMVC