水军大提督 发表于 2024-6-9 13:28:55

SpringBoot中的WebMvcConfigurationSupport和WebMvcConfigurer

在SpringBoot中可以通过以下两种方式来完成自界说WebMvc的设置:
(1)继承WebMvcConfigurationSupport类
(2)实现WebMvcConfigurer接口
通过这两种方式完成的WebMvc设置存在差别,本文将对此作简单阐明与区分。
一.继承WebMvcConfigurationSupport类
1.WebMvcConfigurationSupport是一个用于实现自界说WebMvc设置的类,里面提供了如添加自界说拦截器、消息转换器等方法,同时对于部门方法具有默认实现。
https://img-blog.csdnimg.cn/direct/48623d989a1a4ebebdebcce313e47ca3.png
 2.WebMvcConfigurationSupport中很多方法都被打上@Bean注解,这代表这些方法提供的对象将会交给IOC容器管理。但是WebMvcConfigurationSupport这个类却并未被打上@Configuration注解
https://img-blog.csdnimg.cn/direct/4c129b3846e242faabc62c814e4769b1.png 因此在利用继承WebMvcConfigurationSupport的方式来完成自界说WebMvc设置时,需要手动给打上@Configuration注解,如许WebMvcConfigurationSupport中的@Bean方法才能见效。
https://img-blog.csdnimg.cn/direct/fc409f0b05aa4139945a33487d97601a.png
3. SpringBoot对SpringMvc已经做了自动设置。默认环境下,SpringBoot会利用本身的WebMvc设置类WebMvcAutoConfiguration,而该类实现自动设置的关键在于其内部类EnableWebMvcConfiguration
https://img-blog.csdnimg.cn/direct/e735898a49454456aafadab15dd00bc1.png
可以看到作为WebMvcAutoConfiguration内部类的EnableWebMvcConfiguration又是DelegatingWebMvcConfiguration这个类的子类,那么我们再到DelegatingWebMvcConfiguration这个类内部看一下
https://img-blog.csdnimg.cn/direct/9b5c4a8259f9411b8b221264dfe5cfc0.png
可以看到DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport。
在Spring框架中,我们可以通过继承WebMvcConfigurationSupport类的方式来实现自界说WebMvc设置,而SpringBoot底层正是通过这种方式来完本钱身的WebMvc设置。也就是说我们在SpringBoot中,实际上已经在利用SpringBoot为我们提供的,不同于Spring框架默认设置的WebMvc设置。
4.假如本身利用继承WebMvcConfigurationSupport的方式来实现自界说WebMvc设置,则会覆盖SpringBoot提供的WebMvc设置。
(1)我们在SpringBoot的WebMvc设置类WebMvcAutoConfiguration类上可以看到这一行注解:@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
https://img-blog.csdnimg.cn/direct/5ca4ae8a0bae46518b0a27690fb1c9ef.png
该注解的意思是:在SpringBoot实现自动装配时,假如IOC容器中不存在类型为WebMvcConfigurationSupport的bean,才会将这个WebMvcAutoConfiguration实例化。
(2)而假如我们利用继承WebMvcConfigurationSupport类的方式来实现自界说WebMvc设置,则在自动装配时,将导致WebMvcAutoConfiguration无法实例化,其内部初始化设置将全部无法实例化,也就是说SpringBoot提供的WebMvc设置失效;取而代之的,我们本身实现的WebMvcAutoConfiguration类将作为全部WebMvc设置的源头。
(3)假如我们采用这种方式实现WebMvc设置,且在子类中只重写本身需要拓展的功能方法,则其他方法将采用WebMvcConfigurationSupport的默认方法,其默认方法有的是空方法,而有的给了默认实现。但需要注意的是,WebMvcConfigurationSupport那些给了默认实现的方法,与SpringBoot的WebMvc设置类WebMvcAutoConfiguration本身实现的方法是不一样的。我们假如继承WebMvcConfigurationSupport,而不重写其方法,那么我们就是在利用Spring提供的WebMvc设置,而不是SpringBoot提供的WebMvc设置。
(4)因此,利用继承WebMvcConfigurationSupport类实现自界说WebMvc设置这种方式的缺点是显而易见的——我们将为了拓展部门WebMvc设置,而抛弃全部SpringBoot已经为我们实现的便捷的WebMvc设置。

二.实现WebMvcConfigurer接口
1.WebMvcConfigurer是一个用于实现自界说WebMvc设置的接口,里面同样提供了如添加自界说拦截器、消息转换器等方法,且都是默以为空实现的方法。
https://img-blog.csdnimg.cn/direct/d734acc34f3b40bd8855e1098c8c7396.png
2.利用实现WebMvcConfigurer接口的方式来实现自界说WebMvc设置,并不会覆盖SpringBoot本身实现的设置。
(1)上文提到,只有在IOC中已经有类型为WebMvcConfigurationSupport的bean的时间,SpringBoot的WebMvc设置类WebMvcAutoConfiguration才不会见效。
(2)而假如我们利用实现WebMvcConfigure接口r的方式自界说WebMvc设置,则WebMvcAutoConfiguration可以被实例化,SpringBoot本身设置的WebMvc乐成见效。
(3)而假如在实现WebMvcConfigurer接口时,我们本身实现的方法在SpringBoot的WebMvcAutoConfiguration中也被实现了,则会归并而不是覆盖。这阐明了实现WebMvcConfigurer接口是一种增量式的设置方式:
当我们实现WebMvcConfigurer接口并重写其中的某个方法时,实际上是在提供额外的设置或定制逻辑,这些设置或逻辑将与SpringBoot的默认设置归并。SpringBoot会先加载其默认的自动设置,然后应用你在WebMvcConfigurer中界说的自界说设置。
3.继承WebMvcConfigurationSupport与实现WebMvcConfigurer是不同的设置逻辑
(1)继承WebMvcConfigurationSupport类是抛弃所有SpringBoot提供的WebMvc设置,全部由本身实现。
(2)实现WebMvcConfigurer接口是在保存SpringBoot提供的WebMvc设置的基础上,添加本身要拓展的设置。
(3)因此,实际开辟中更保举利用实现WebMvcConfigurer接口的方式来完成自界说WebMvc设置。





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: SpringBoot中的WebMvcConfigurationSupport和WebMvcConfigurer