springboot mvc自动配置问题?

其中的自动配置类WebMvcAutoConfiguration里面的两个内部配置类:1:WebMvcAutoConfigurationAdapter;2:EnableWebMvcConfiguration。
然后WebMvcAutoConfigurationAdapter上有个注解:@Import(EnableWebMvcConfiguration.class),有两个疑问:
1:为啥EnableWebMvcConfiguration已经是个配置类了会随着WebMvcAutoConfiguration加载而一起加载被注册到容器中,为啥下面在WebMvcAutoConfigurationAdapter这个内部配置类中还要用@Import注解导一次,这个不是造成重复注册bean了吗?
2:WebMvcAutoConfiguration上有个@ConditionnalOnMissingBean(WebMvcConfigurationSupport.class)注解,他自己本身的内部配置类EnableWebMvcConfiguration就是这个类型的子类,而且下面WebMvcAutoConfigurationAdapter又用@Import(EnableWebMvcConfiguration.class)把他给导入容器了,这样这个配置类不是就不生效?这不是自相矛盾吗?1.png2.png

阅读 2.3k
2 个回答
  • 第一个感觉就是实现如果不自动扫描@Configuration的情况下也可以加载EnableWebMvcConfiguration吧,而且注册时内部有判定,不会重复注册bean
  • 第二个没什么矛盾吧,WebMvcConfigurationSupport本身不是bean,只有扩展它然后注入容器才是相关的bean,所以如果有自定义的beanWebMvcAutoConfiguration不生效、内部类EnableWebMvcConfiguration也不生效,相关自动配置失效;如果没有自定义bean,此时WebMvcAutoConfiguration已经起作用,不管内部类EnableWebMvcConfiguration再实现任何WebMvcConfigurationSupport相关的bean都对WebMvcAutoConfiguration本身没影响了啊。

WebMvcAutoConfigurationAdapter有个@Order(0)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题