SpringBoot扩展SpringMVC原理并实现全面接管
如果想在SpringBoot中扩展一些SpringMVC的配置,例如需要配置自定义的视图解析器或拦截器等,需要怎么实现呢?
例如,自定义一个视图解析器:
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
我们只需要编写一个配置类去实现WebMvcConfigurer接口,并选择实现接口中的方法,不能标注@EnableWebMvc,这些WebMvcConfigurer接口中的方法就是SpringMVC所可以扩展的配置
注意:在SpringBoot1.0版本中扩展SpringMVC配置是继承WebMvcConfigurerAdapter类,但在2.0以上的版本中已经过时,官方推荐使用以上实现WebMvcConfigurer接口的方式进行扩展,因为在2.0版本中WebMvcConfigurer接口有了默认实现。
WebMvcConfigurer方法介绍:这里只列举几个比较关键的方法
public interface WebMvcConfigurer {
//定制URL匹配规则
default void configurePathMatch(PathMatchConfigurer configurer) {
}
//内容协商机制
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
//异步任务执行器。
default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
}
//使用默认servlet处理静态资源
default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
//添加格式转换器
default void addFormatters(FormatterRegistry registry) {
}
//添加拦截器
default void addInterceptors(InterceptorRegistry registry) {
}
//添加视图解析器
default void addViewControllers(ViewControllerRegistry registry) {
}
}
扩展MVC的实现原理:
我们都知道WebMvcAutoConfiguration是SpringMVC的自动配置类,当在做其他配置导入时,导入了@Import(EnableWebMvcConfiguration.class)这样一个注解,这个注解有什么用?
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
}
点进这个注解,发现他还是WebMvcAutoConfiguration里的一个静态内部类,但他继承了DelegatingWebMvcConfiguration
@Configuration(proxyBeanMethods = false)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
}
再点进这个DelegatingWebMvcConfiguration类里,开头有这样一段代码,有一个configurers属性,类型是WebMvcConfigurerComposite ,这个WebMvcConfigurerComposite类也实现了WebMvcConfigurer,当@Autowired标注在一个方法上说明,这个方法的参数都从容器中获取,这里是从容器中获取所有的WebMvcConfigurer,并赋值给了configurers属性
@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
}
}
}
在这个类往下看,发现这个类的方法跟WebMvcConfigurer接口里的方法一样,以这个视图解析器举例,方法里调用了这个方法this.configurers.addViewControllers(registry)
@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
}
}
...
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
this.configurers.addViewControllers(registry);
}
}
点进configurers.addViewControllers(registry),这个方法是把容器中所有的addViewControllers()都执行一遍。<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">因为我们自己写的配置类也注入到了容器里,所以我们的配置也会被调用,并且也被SpringBoot自动配置上,所以SpringMVC的自动配置和我们的扩展配置都会起作用</mark>;
class WebMvcConfigurerComposite implements WebMvcConfigurer {
...
@Override
public void addViewControllers(ViewControllerRegistry registry) {
for (WebMvcConfigurer delegate : this.delegates) {
delegate.addViewControllers(registry);
}
}
}
还有上面在写自定义配置类时为什么不能标注@EnableWebMvc
因为一但标注了@EnableWebMvc,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了。<mark style="box-sizing: border-box; outline: 0px; background-color: rgb(248, 248, 64); color: rgb(0, 0, 0); overflow-wrap: break-word;">原理又是怎么样的?</mark>
给自己的配置类加上@EnableWebMvc
@Configuration
@EnableWebMvc
public class myConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
这个注解导入了@Import(DelegatingWebMvcConfiguration.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
这个类继承了WebMvcConfigurationSupport
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
}
我们再回头看一下WebMvcAutoConfiguration,@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个注解的意思就是容器中没有这个组件的时候,这个自动配置类才生效
小结:大概了解到SpringBoot扩展SpringMVC的原理和全面接管SpringMVC,但SpringBoot中还有其他很多配置,只要了解其中的原理,其他配置也就一通百通了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
代码知识SEO上一篇 : 详解iOS Method Swizzling使用陷阱
下一篇 : 详解在SpringBoot中使用MongoDb做单元测试的代码
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!