zoukankan      html  css  js  c++  java
  • 注解@EnableWebMvc如何导致web中的SpringMvc失效

    全面接管Spring MVC

    是指SpringBoot对SpringMVC的自动配置,不需要了
    所有的,SpringMVC的自动配置都失效了
    所有的,都需要自己配置

    @EnableWebMvc

    需要在配置类中添加@EnableWebMvc即可

    //使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
    @EnableWebMvc   //全面接管SpringMVC
    @Configuration
    public class MyMvcConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            // super.addViewControllers(registry);
            //浏览器发送 /atguigu 请求来到 success
            registry.addViewController("/atguigu").setViewName("success");
        }
    }

    实现原理

    WebMvcAutoconfiguration.java没有  @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个场景才会生效

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

    看它的继承树

     而@EnableWebMvc点进去,发现导入了DelegatingWebMvcConfiguration.class,而DelegatingWebMvcConfiguration.class的父类就是WebMvcConfigurationSupport.class,所以全部失效,全权接管springMvc

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

      

  • 相关阅读:
    po教学001
    肖sir__ 金牌高级讲师__下载视频方法
    肖sir__ 金牌高级讲师__html下载收费音乐方法
    肖sir_少儿编程了解(001)
    【CF375D】Tree and Queries
    【CF1063F】String Journey
    【洛谷P6071】Treequery
    【ARC122E】Increasing LCMs
    【ARC122C】Calculator
    【牛客练习赛84 E】牛客推荐系统开发之标签重复度
  • 原文地址:https://www.cnblogs.com/ljstudy/p/14469866.html
Copyright © 2011-2022 走看看