zoukankan      html  css  js  c++  java
  • 【串线篇】spring boot全面接管springMvc

    一、Spring MVC auto-configuration

    Spring Boot 自动配置好了SpringMVC

    以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoConfiguration

    Inclusion of:

    ContentNegotiatingViewResolver and  BeanNameViewResolver

    自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何 渲染(转发?重定向?))

    ContentNegotiatingViewResolver:组合所有的视图解析器的;

    如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;

    ~Support for serving static resources, including support for WebJars (see below).静态资源文件夹路径,webjars

    ~Static index.html support. 静态首页访问

    ~Custom Favicon support (see below). favicon.ico

    ~自动注册了 of            Converter     ,          GenericConverter                 ,       Formatter           beans. 

    Converter:转换器; public String hello(User user):类型转换使用Converter

    Formatter 格式化器; 2017.12.17===Date;

        @Bean
        @ConditionalOnProperty(prefix  =  "spring.mvc",  name  =  "date‐format")//在文件中配置日期格式化的规则
        public  Formatter<Date>  dateFormatter()  {
        return  new  DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件     
    }

    二、扩展SpringMVC

    <mvc:view‐controller  path="/hello"  view‐name="success"/>
    <mvc:interceptors>
    <mvc:interceptor>
    <mvc:mapping  path="/hello"/>
    <bean></bean>
    </mvc:interceptor>
    </mvc:interceptors>

    编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc; 既保留了所有的自动配置,也能用我们扩展的配置

     

    原理:

    效果:SpringMVC的自动配置和我们的扩展配置都会起作用;

    三、全面接管SpringMVC;

    SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了 我们需要在配置类中添加@EnableWebMvc即可;

    原理:

    //使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能

    @EnableWebMvc @Configuration

    public  class  MyMvcConfig  extends  WebMvcConfigurerAdapter  {

    @Override

    public  void  addViewControllers(ViewControllerRegistry  registry)  {

    //  super.addViewControllers(registry);

    // 浏 览 器 发 送 /atguigu 请 求 来 到 success registry.addViewController("/atguigu").setViewName("success");

    }

    }

    为什么@EnableWebMvc自动配置就失效了;

    1)@EnableWebMvc的核心

        @Import(DelegatingWebMvcConfiguration.class)
        public  @interface  EnableWebMvc  {

    2)

        @Configuration
        public  class  DelegatingWebMvcConfiguration  extends  WebMvcConfigurationSupport  {

    3)

        @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  {

    4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;

    5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;



  • 相关阅读:
    人工智能深度学习入门练习之(6)求和方法2
    人工智能深度学习入门练习之(5)求和
    人工智能深度学习入门练习之(4)矩阵实现
    人工智能深度学习入门练习之(3)求导
    阿里巴巴Java开发手册(格式规约篇)——查自己的漏-补自己的缺
    阿里巴巴Java开发手册(命名规范/常量定义篇)——查自己的漏-补自己的缺
    自学git
    IE浏览器兼容性问题输出
    项目经验输出
    Ping任务管理模块-JavaWeb
  • 原文地址:https://www.cnblogs.com/yanl55555/p/12091572.html
Copyright © 2011-2022 走看看