SpringBoot 项目 @EnableWebMvc 注解应用的总结

4 篇文章 0 订阅

在spring-boot+spring mvc 的项目中,有我们需要自己配置一些项目的设置,就会涉及到三个类(@EnableWebMvc 、WebMvcConfigurationSupport和WebMvcConfigurationAdapter),那么,他们之间有什么关系呢? 
首先,@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法。
翻源代码:

 @EnableWebMvc
 
 *  @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer
 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
 * @see org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration

有以下几种使用方式:

@EnableWebMv + extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可(友情提示,最新版spring boot 因为jdk已经支持接口默认方法实现,所以WebMvcConfigurationAdapter已经过时),这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置

extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置

extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式依旧使用springboot的@EnableAutoConfiguration中的设置
 

有时候我们我们需要自定义一些配置

比如:Spring Boot 定制HTTP消息转换器

 

总结

使用 @EnableWebMvc 注解会屏蔽springboot的@EnableAutoConfiguration,需要自己设置MessageConverter;

默认使用 @EnableAutoConfiguration 注解,读取 application.properties 或 application.yml 文件中的配置。

 

参考

https://blog.csdn.net/testcs_dn/article/details/80249894

https://blog.csdn.net/pomer_huang/article/details/77170673

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值