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中还有其他很多配置,只要了解其中的原理,其他配置也就一通百通了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
java实现附件预览(openoffice+swftools+flexpaper)实例
本篇文章主要介绍了java实现附件预览(openoffice+swftools+flexpaper)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。2016-10-10
使用Java将DOCX文档解析为Markdown文档的代码实现
在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文档仍然以Microsoft Word的DOCX格式保存,本文将介绍如何使用Java和相关库将DOCX文档解析为Markdown文档,需要的朋友可以参考下2025-04-04
Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)
本文主要介绍Tomcat启动分析的知识,这里整理了相关资料及分析原因和如何实现的方法,有兴趣的小伙伴可以参考下2016-09-09
MyBatis/mybatis-plus项目打印SQL的方法实现
SpringBoot项目中,经常需要打印SQL语句及其参数,本文就来介绍一下MyBatis/mybatis-plus项目打印SQL的方法实现,具有一定的参考价值,感兴趣的可以了解一下2024-07-07
Java异常--常见方法--自定义异常--增强try(try-with-resources)详解
这篇文章主要介绍了Java异常--常见方法--自定义异常--增强try(try-with-resources)的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-03-03
springcloud gateway聚合swagger2的方法示例
这篇文章主要介绍了springcloud gateway聚合swagger2的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-04-04


最新评论