springboot中WebMvcConfigurationSupport、WebMvcConfigurationAdapter区别

1、springboot默认可以访问以下路径文件(见ResourceProperties):
    classpath:/static
    classpath:/public
    classpath:/resources
    classpath:/META-INF/resources
   当使用了@EnableWebMvc时,默认的静态资源访问无效了因为默认情况下mvc使用的配置是WebMvcAutoConfiguration,加入该配置变成了WebMvcConfigurationSupport
2、@EnableWebMvc、WebMvcConfigurationSupport、WebMvcConfigurationAdapter
    @EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法
    @EnableWebMvc+extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的WebMvcAutoConfiguration中的设置
    @EnableWebMvc+extends WebMvcConfigurationSupport 只会使用@EnableWebMvc
    extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@WebMvcAutoConfiguration中的设置
    extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式依旧使用springboot的WebMvcAutoConfiguration中的设置
    在springboot2.x中,WebMvcConfigurationAdapter已经过时,通过实现接口WebMvcConfigurer可以替代原有规则

在默认情况下,springboot是启用WebMvcAutoConfiguration,这点可以在spring-boot-autoconfigure.jar/META-INF/spring.factories中看到

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

但是打开WebMvcAutoConfiguration可以看到

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
      WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
      ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration

其中@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)说明,当没有WebMvcConfigurationSupport对应的bean时,才会使用该配置,所以当我们使用继承WebMvcConfigurationSupport的方式类扩展mvc时,原有的配置则无效。

同时可以看下@EnableWebMvc

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

其中@Import(DelegatingWebMvcConfiguration.class)为该注解的核心,

@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的子类,但是相对而言,添加了自己的扩展配置,同时从setConfigurers可以看到,所有WebMvcConfigurer的子类也会被添加到配置中。

4691d1e1710ff4e0bacfcc17a85a883efca.jpg

其中WebMvcConfigurerAdapter,也是WebMvcConfigurer的子类,这就是为什么我们使用@EnableWebMvc+WebMvcConfigurer的方式可以实现EnableWebMvc的配置加上自己的配置了。

转载于:https://my.oschina.net/suspring/blog/3039633

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值