spring boot @EnableWebMvc禁用springMvc自动配置原理。

说明:

  在spring boot中如果定义了自己的java配置文件,并且在文件上使用了@EnableWebMvc 注解,那么sprig boot 的默认配置就会失效。如默认的静态文件配置路径:"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/",将失效。而有效的配置将只有自己写的java配置 。

原理浅析:

 1. 那么他是怎么将默认的配置都禁用的,跟踪源码分析下 ,首先看看@EnableWebMvc 这个注解:

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

2.注解导入DelegatingWebMvcConfiguration类,而DelegatingWebMvcConfiguration 继承 WebMvcConfigurationSupport 类

 

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...//省略
}

3.来看看springMvc的自动配置类:

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

看到标记黄色部分代码没,这个注解是条件注解,表示,如果容器中不存在 WebMvcConfigurationSupport这个类, WebMvcAutoConfiguration 配置类才会

才spring 加载。而我们使用注解@EnableWebMvc就把 WebMvcAutoConfiguration 这个类加载到了spring容器中 。所以 WebMvcAutoConfiguration 默认配置类将失效。

 

转载于:https://www.cnblogs.com/jonrain0625/p/11300569.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值