先放出来几个类(包含注解或接口)来观摩一下
WebMvcConfigurer
@EnableWebMvc
WebMvcConfigurerAdapter
(已过时,不再详述,可以理解为继承该类有和实现WebMvcConfigurer一样的效果)WebMvcConfigurationSupport
WebApplicationInitializer
这里只聊springboot
或者无web.xml
环境的情况,无论如何得看一下这个祖宗,以下代码来源于spring官网
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletCxt) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}
这个是一切无配置文件spring
和springmvc
整合的基础,用来替代原始的web.xml
中的ContextLoadListener
和DispatcherServlet
,两者效果等同;
现在我们先基于上述代码的情况来说,请注意以下结论的前提是基于上述示例代码,很重要,其实就像是很久之前我们从零开始搭建整合方案一样,现在只是配置了整合的类,还没有功能,如果我们想要再配置一个json
的消息转换器,那么我们就会有如下几种方案
- 继承
WebMvcConfigurationSupport
- 实现
WebMvcConfigurer
- 继承
WebMvcConfigurerAdapter
(已过时不再详述)
继承WebMvcConfigurationSupport
和实现WebMvcConfigurer
的区别如下
-
WebMvcConfigurationSupport
直接继承并使用@Configuration
标识即可,而实现WebMvcConfigurer
则需要标识为注解@Configuration
以外还需要使用注解@EnableWebMvc
标识才可,所以一个项目中可以有多个地方去实现这个接口,只要标识为配置类。然后在一处地方开启@EnableWebMvc
就可。这里提前说一下如果是springboot
环境这里还大有说头,这里还有个大坑,留在后面说 -
WebMvcConfigurationSupport
更偏向底层,可以定制化的功能更多,而WebMvcConfigurer
是一个接口,是针对WebMvcConfigurationSupport
功能将一些常用的功能选择性的暴露出来,实际上WebMvcConfigurer
是依赖于WebMvcConfigurationSupport
来实现功能添加的
为什么说WebMvcConfigurer
是依赖于WebMvcConfigurationSupport
来实现功能添加的?我们来看一下配合该接口的注解@EnableWebMvc
源码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
来看一下这个注解导入的配置类DelegatingWebMvcConfiguration
为何物?以下摘取该类部分源码
@Configuration
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);
}
}
}
首先这个配置类其实就是我们上面最开始说的用继承来扩展功能的WebMvcConfigurationSupport
,那么为什么实现WebMvcConfigurer
接口也行?注意看上图中的代码,提供了一个configurers
属性, 然后通过setConfigurers
方法注入将ioc
容器中的所有实现了WebMvcConfigurer
接口的配置类都添加到configurers
中;后续实现代码不是本章讨论范围,我也没有往下看,单看这里其实就已经明白了。
**上述是整合的基础,那么当我们在springboot
中要添加功能的时候要注意一些什么事情呢?**这个也是写这篇文章的目的,因为最近在项目中有人在扩展功能的时候去继承了WebMvcConfigurationSupport
这个类,然后联想到之前的项目也有人在springboot
项目中使用了注解@EnableWebMvc
,这两种情况都不会导致项目启动报错,但却在不该使用的时候使用了这些功能,导致了项目其实是不能正常使用的。现在来看一下为什么?
首先看一下springboot
给我们提供的自动整合类,请参考类org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
,我来截取部分代码
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
@Configuration
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {
}
/**
* Configuration equivalent to {@code @EnableWebMvc}.
*/
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
}
}
现在说一下结论,springboot
为我们提供的整合功能,已经默认的帮我们添加了很多功能,如消息转换器,静态资源映射,视图解析器,看下WebMvcAutoConfiguration
的内部类WebMvcAutoConfigurationAdapter
,其实就是实现了接口WebMvcConfigurer
,然后再通过注解@Import(EnableWebMvcConfiguration.class)
又将EnableWebMvcConfiguration
这个配置类导入了进来,而我们点进去发现这个类的作用其实就是等同于之前我们说过的@EnableWebMvc
。因此我们说的消息转换器啊,静态资源映射,视图解析器等这些默认实现就在WebMvcAutoConfiguration
的内部类WebMvcAutoConfigurationAdapter
又通过@Bean
注入进来的
也就是说springboot
其实帮我们整合好之后又默认帮我们做了一切常用的实现,这样我们开箱即用的不仅是整合好的框架,还有一些约定大于配置的功能,如静态资源要放在static
下,其实就是默认帮我们做了资源映射,详细可以看下org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#addResourceHandlers
默认整合的功能基本满足于我们日常的开发,而如果我们还需要添加功能要怎么办呢?其实就是直接实现接口WebMvcConfigurer
然后将当前类使用注解@Configuration
标识为配置类即可。
那么为什么不能再继续继承接口WebMvcConfigurationSupport
了呢?还是来看一下我们的自动配置类WebMvcAutoConfiguration
吧,仔细看一下上面的配置类上的条件表达式中有这么一句非常非常重要的@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
,
上面我们所属的所有整合功能的前提是当前ioc
容器中没有WebMvcConfigurationSupport
这个bean
,我们看到了springboot
自己的实现是在当前自动配置类生效的时候才通过实现接口WebMvcConfigurer
的,所以容器中在当前配置类未执行之前也是没有这个WebMvcConfigurationSupport
的,现在我们突然在项目中添加功能的时候去继承了这个类,然后标识为配置类之后,立马在容器中就出现了这个bean
,然后springboot
就会以为我们要全面接管整合springmvc
,我们要抛弃它的默认实现,然后自己玩。然后就悲剧了。现在整个mvc
中反而只有我们自己新加的这个扩展空间了。这在绝大多数情况下根本不是我们想要的。
还有一个问题,为什么加注解@EnableWebMvc
也不行?
其实通过上面的讲解我们已经能够看出来,这个注解其实就是导入了DelegatingWebMvcConfiguration
这个配置类,而这个类就是继承WebMvcConfigurationSupport
的,这两个效果是相同的,所以也不行。
总而言之一句话,在WebMvcAutoConfiguration
这个配置类执行之前,无论是继承WebMvcConfigurationSupport
还是在某个配置类上添加了注解@EnableWebMvc
,都会导致容器中会被注入类型为WebMvcConfigurationSupport
的bean
,而springboot
在实现自动配置时将这种行为定义成了你要自己去实现``