SpringBoot-第8讲之SpringBoot中SpringMVC自动配置原理

SpringBoot中SpringMVC自动配置原理


前言

参考SpringBoot官方文档:https://docs.spring.io/spring-boot/docs/2.3.5.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

SpringBoot自动配置好了SpringMVC ,下面的图片是SpringMVC的默认配置(接下来就来一一讲解):


一、SpringMVC自动配置

1、Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

        自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?)ps:渲染可以简单解释为:浏览器由于不能执行并显示jsp文件,所以需要omcat服务器将jsp文件渲染成html文件,以便于能够在浏览器中将http url请求的结果进行显示。)

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

如何定制一个视图解析器:我们可以通过在容器中添加一个视图解析器,SpringBoot就将自动组合进来。

@SpringBootApplication
public class SpringBoot07WebApplication{
     public static void main(String[] args){
       SpringApplication.run(SpringBoot07WebApplication.class,args);

}
     @Bean
     public ViewResolver myView(){
        return  new myView();
}
     @Overrile
     private view resolveViewName(String viewName,Locale locale) throwsException{
     return null;
}
}

2、Static index.html support.-静态首页访问

3、Support for serving static resources, including support for WebJars (covered later in this document)).-静态资源文件夹路径

4、Automatic registration of Converter,GenericConverter, and Fomatter beans.

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

     Formatter:格式化器。2019.17.17=Date 转换日期。

@Bean
@ConditionalOnProperty(prefix = "spring.mvc",name = "data-format")//在文件中配置日志格式化的规则
public Formatter<Data> dataFormatter(){
   return new DataFormatter(this.mvcProperties.getDataFormat());//日期格式化组件
} 
//自己添加的格式化转换器,我们只需要放在容器中即可

5、Support for HttpMessageConverters.

   HttpMessageConverter:SpringMVC用来转换http请求和响应的

   HttpMessageConverters是从容器中确定的,获取所有的HttpMessageConverter。同理只需要在容器中添加  HttpMessageConverter。

import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import org.springframework.boot.jackson.*;

@JsonComponent
public class Example {

    public static class Serializer extends JsonSerializer<SomeObject> {
        // ...
    }

    public static class Deserializer extends JsonDeserializer<SomeObject> {
        // ...
    }

}

6.Automatic use of a ConfigurableWebBindingInitializer bean .

    我们可以配置一个configurationWebBindingIntializer来替换默认的,需要添加到容器中。

    初始化WebDataBinder
    请求数据---JavaBean

7、Automatic registration of MessageCodeResolver.-定义错误代码生成规则

    ps:org.springframework.boot.autoconfiguration.web:web的所有自动场景

二、扩展SpringMVC(SpringBoot 2.x 后WebMvcConfigurationAdapter 过时)

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

编写一配置类(@Configuration),是WebMvcConfiguratiionAdapter类型,不能标注@EnableWebMvc。既保留了所有的自动配置,也能用我们扩展的配置。但是SpringBoot 2.x后WebMvcConfiguratiionAdapter过时了,但我们可以实现WebMvcConfigurer接口,这个接口的方法都加了jdk1.8的 default方法修饰,不强制实现所有的方法,可以根据实际实现相关的方法。继承了WebMvcConfigurationSupport。因为继承了WebMvcConfigurationSupport,相当于容器中已经有了WebMvcConfigurationSupport,所以默认配置都不会生效,都得自己在配置文件中配置。

三、如何修改SpringBoot的默认配置?  

    模式:

     1)SpringBoot在中配置很多组件的时候,先看看容器中有没有用户自动配置的(@Bean/@Component),如果有就用用户配置的。如果没有,才自动配置。如果有些组件可以有多个(ViewResolver)可以结合两者。

     2)在SpringBoot中会有非常多的XXXconfiguration,需要多留心。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值