SpringMVC的自定义配置和自动化配置

SpringBoot的自动配置MVC处理加载逻辑

基于Spring Boot的MVC自动化配置由WebMvcAutoConfiguration类完成,部分关键源码:

@AutoConfiguration(after = { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
        ValidationAutoConfiguration.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
public class WebMvcAutoConfiguration {

    //省略源码......

    @SuppressWarnings("deprecation")
    @Configuration(proxyBeanMethods = false)
    @Import(EnableWebMvcConfiguration.class)
    @EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
    
     //省略源码......

    }

    @Configuration(proxyBeanMethods = false)
    @EnableConfigurationProperties(WebProperties.class)
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
       
     //省略源码......

    }
}
  • 自动化配置类(带有注解@AutoConfiguration)可以通过/META-INF/spring.factories文件引入

  • 从WebMvcAutoConfiguration的类级注解@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })可以看出类路径下的Servlet、DispatcherServlet和WebMvcConfigurer类会触发SpringBoot的自动化配置

  • 从WebMvcAutoConfiguration的类级注解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)可以看出,自定义配置的WebMvcConfigurationSupport类会使得SpringBoot的MVC自动化配置失效,那么自定义配置的WebMvcConfigurationSupport将全面接管MVC的配置

  • WebMvcAutoConfigurationAdapter(WebMvcAutoConfiguration的静态内部类)会引入EnableWebMvcConfiguration配置类(WebMvcAutoConfiguration的静态内部类),EnableWebMvcConfiguration是DelegatingWebMvcConfiguration的一个子类,在DelegatingWebMvcConfiguration类中会自动注入所有的MVC配置类WebMvcConfigurer,DelegatingWebMvcConfiguration的部分源码如下:

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    //省略源码......
    @Autowired(required = false)
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }
    }
   //省略源码......
 }

使用@EnableWebMvc配置MVC

从注解源码可以看出@EnableWebMvc引入一个DelegatingWebMvcConfiguration类实现MVC的配置,DelegatingWebMvcConfiguration的引入会使得SpringBoot的MVC自动化配置全部失效(因为DelegatingWebMvcConfiguration是WebMvcConfigurationSupport的子类,前文已经讲述过原因),所有的MVC相关的配置都需要手动完成,所以SpringBoot项目不建议使用@EnableWebMvc处理SpringMVC的配置。

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

}

那么怎样既可以保留SpringBoot关于MVC的自动化配置又能自定义扩展配置呢,不要使用不使用@EnableWebMvc注解注解,同时有以下两种方式扩展配置:

  1. 直接实现WebMvcConfigurer接口实现MVC自定义扩展配置(建议)

  1. 直接继承WebMvcConfigurerAdapter实现MVC自定义扩展配置

这里需要注意的是实现WebMvcConfigurer接口和继承WebMvcConfigurerAdapter是一样的效果,其实WebMvcConfigurerAdapter就是WebMvcConfigurer接口的一个空实现,因为Java8之后接口中可以存在default方法,WebMvcConfigurerAdapter中的空方法可以直接定义为WebMvcConfigurer中的default方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豢龙先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值