• 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 默认配置类将失效。

  • 相关阅读:
    Codeforces758C
    Codeforces758B
    CodeForces758A
    数据库实习
    数据结构实习-迷宫(基于Qt实现)
    CodeForces757B
    CodeForces757A
    HDU3790
    数据结构实习
    Codeforces374B
  • 原文地址:https://www.cnblogs.com/jonrain0625/p/11300569.html
Copyright © 2020-2023  润新知