SpringBoot专题学习Part14:SpringBoot下的SpringMVC自动配置的内容

Spring Boot已经在内部自动配置好了SpringMVC
那么 具体是自动配置了哪些内容
让我们来了解:

注:英文内容是直接从官网扒的

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    自动配置了ViewResolver(视图解析器) 根据方法的返回值得到视图对象View 然后视图对象决定如何渲染 是转发还是重定向
    ContentNegotiatingViewResolver的作用:组合所有的视图解析器
    若要自己定制 需自己给容器中添加一个视图解析器 然后ContentNegotiatingViewResolver会自动将其组合进来

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

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

  • Custom Favicon support (see below).
    favicon.ico图标

  • Automatic registration of Converter, GenericConverter, Formatter beans.
    自动注册了Converter GenericConverter Formatter的beans
    Converter组件:转换器 当类型转换的时候使用Converter
    例:将文本数据封装成对象中的数据 须经过类型转换
    Formatter组件:格式化器
    例:将2017.12.17字符串转换为Date
    底层是:

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

因而 需要在配置文件中用date-format配置日期格式化的规则 否则不会生效
自己也可添加格式化器转换器 只需将其放在容器中即可

  • Support for HttpMessageConverters (see below).
    支持HttpMessageConverters
    HttpMessageConverters:消息转换器 SpringMVC用于转换Http请求和响应的
    例:将User转换为JSON
    MessageConverters是从容器中确定的 底层从容器中获取所有的HttpMessageConverters
    自己也可给容器中添加HttpMessageConverter 只需将自己的组件注册到容器中(用@Component和@Bean注解)
  • Automatic registration of MessageCodesResolver (see below).
    定义错误代码生成规则
  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).
    自动使用ConfigurableWebBindingInitializer
    可配置一个ConfigurableWebBindingInitializer添加到容器中来替换默认的
    用途:初始化WebDataBinder(Web数据绑定器)
    功能:将请求的数据绑定到JavaBean中

如何修改SpringBoot的默认配置:

方式有好几种
其中一种方式是:SpringBoot中的很多组件在自动配置的时候 会先看容器中有没有用户配置的(用到@Configuration注解和@Bean注解)
若有 则使用用户配置的 若没有 则使用自动配置
部分组件若支持多个 则会将用户配置的和自带的合并起来
因此 只需自己创建配置类 并加上注解加入到容器中即可


发布了174 篇原创文章 · 获赞 5 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/Piconjo/article/details/104911479
今日推荐