SpringBoot 继承WebMvcConfigurationSupport后导致springMVC的自动配置失效问题

现象

自己写了一个配置类,继承WebMvcConfigurationSupport,在其中实现自己的业务代码,项目运行后,突然发现springMVC的自动配置都失效了。
在这里插入图片描述

原因

在这里插入图片描述
根据上图SpringBoot的官方文档中翻译到:如果想保持Spring Boot MVC原本的配置(自动配置)并且又想增加自己的配置,那么需要在自己的标注@Configuration的类上面添加 @EnableWebMvc这个注解

@EnableWebMvc
这个注解的作用就是通过import引入 @Configuration 注解的类——DelegatingWebMvcConfiguration,然后DelegatingWebMvcConfiguration这个类又继承了 WebMvcConfigurationSupport这个类。
最后在在WebMvcAutoConfiguration这个类里看到有:@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个注解的意义是,当没有WebMvcConfigurationSupport这个类的时候,WebMvcAutoConfiguration(也就是SpringMvc的自动配置类)才会生效。

总结

如果你自己写配置类,继承WebMvcConfigurationSupport,那就要使用@EnableWebMvc来使它的自动配置失效了。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_28545605/article/details/126874651