Spring Boot中Spring MVC自动配置原理,扩展与全面接管

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Java_Glory/article/details/89839891

Spring Boot 自动配置好了 Spring MVC:

以下是Spring Boot 对 Spring MVC 的自动配置 : (WebMvcAutoConfiguration)

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.   

       //自动配置了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(浏览器小图标)
  • 自动注册了ConverterGenericConverterFormatter beans.

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

      //Formatter : 格式化器 --- 2019/01/01===Date

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

      //自己添加的格式化器,转换器,只需要放在容器中即可

  • Support for HttpMessageConverters (see below).

      //HttpMessageConverters : SpringMVC用来转换Http请求和响应的 ;

      //HttpMessageConverters 是从容器中确定的 ; 获取所有的HttpMessageConverter ;

      //自己给容器中添加HttpMessageConverter , 只需要将自己的组件注册在容器中(@Bean , @Component)

  • Automatic registration of MessageCodesResolver (see below).      //定义错误代码生成规则的
  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).

      //可以配置一个 ConfigurableWebBindingInitializer 来替换默认的 ; (添加到容器)

初始化WebDataBinder ;
请求数据=====JavaBean
org.springframework.boot.autoconfigure.web : web的所有自动配置场景 

扩展Spring MVC : 

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

编写一个配置类( @Configuration ) , 是WebMvcRegistrationsAdapter类型 ; 不能标注@EnableWebMvc

既保留了所有的自动配置 , 也能用扩展的配置 : 

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

原理 : 

     1.WebMvcAutoConfiguration是Spring MVC 的自动配置类

     2.在做其他自动配置时会导入 : @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class}) 

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

    //从容器中获取所有的WebmvConfigurer
    @Autowired(
    required = false
    )
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
            //一个参考实现 : 将所有的WebMvcConfigurer 相关的配置都来一起调用 ;
            //public void addViewControllers(ViewControllerRegistry registry) {
            //    Iterator var2 = this.delegates.iterator();

            //   while(var2.hasNext()) {
            //      WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
            //      delegate.addViewControllers(registry);
            //    }

            //}
            
        }

    }
}

     3.容器中所有的 WebMvcConfigurer 都会一起起作用 ;

     4.自己的配置类也会被调用 ;

     效果 : Spring MVC 的自动配置和扩展配置都会起作用 ;

全面接管SpringMVC :

SpringBoot 对 Spring MVC 的自动配置不需要了 , 所有的都是自己配 , 同时所有 SpringMVC 的自动配置都失效了 ;

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

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

原理 :

     1. @EnableWebMvc的核心

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

     2.点击进入DelegatingWebMvcConfiguration 

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {...}

     3.再看WebMvcAutoConfiguration 

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class, DispatcherServlet.class,WebMvcConfigurerAdapter.class})
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {...}

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

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

如何修改Spring Boot 的默认配置 : 

  1. Spring Boot 在自动配置很多组件的时候 , 先看容器中有没有用户自己配置的( @Bean , @Component )如果有就用用户配置的 , 如果没有 , 才自动配置 ; 如果有些组件可以有多个( ViewResolver ) 将用户配置的和自己默认的组合起来 ; 
  2. 在Spring Boot 中会有非常多的xxxConfigurer进行扩展配置

详情见官方文档 : https://docs.spring.io/spring-boot/docs/1.5.20.RELEASE/reference/htmlsingle/

猜你喜欢

转载自blog.csdn.net/Java_Glory/article/details/89839891