SpringBoot对SpringMVC提供了许多自动配置

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
  • Support for serving static resources, including support for WebJars (covered later in this document)).
  • Automatic registration of ConverterGenericConverter, and Formatter beans.
  • Support for HttpMessageConverters (covered later in this document).
  • Automatic registration of MessageCodesResolver (covered later in this document).
  • Static index.html support.
  • Custom Favicon support (covered later in this document).
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

视图解析器

  包含了ContentNegotiatingViewResolver和BeanNameViewResolver。

  在WebMvcAutoConfiguration中定位到ContentNegotiatingViewResolver有关的方法,该方法标注了@Bean,所以会把ContentNegotiatingViewResolver加入容器。根据注释可以看到,ContentNegotiatingViewResolver用于管理所有的ViewResolver,即所有实现了ViewResolver的Bean,然后对于每个View找到最合适的ViewResolver去解析。我们可以向容器中添加自定义视图解析器,添加的自定义视图解析器也会被ContentNegotiatingViewResolver所管理。

        @Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver",
value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(
beanFactory.getBean(ContentNegotiationManager.class));
// ContentNegotiatingViewResolver uses all the other view resolvers to locate
// a view so it should have a high precedence
resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
return resolver;
}

  在启动类使用@Bean注解添加自定义ViewResolver,并在doDispatch打上断点,随意发送一个请求,观察ContentNegotiatingViewResolver是否把自定义的myViewResolver收录进去。

@SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @Bean
public ViewResolver myViewResolver(){
return new MyViewResolver();
} private static class MyViewResolver implements ViewResolver{ @Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}

静态资源

  

转换器与格式化器

  页面表单提交的数据都是String类型的,当注入pojo的时候需要转换成相应的类型,这一工作由conveter完成。日期格式的转换有转换器完成。SpringBoot自动注入这两种类型的Bean是byType的,即从容器中获取所有Converter Formatter类型的bean并调用相应的add方法,所以如果想添加自定义的转换器、格式化器,同样只需要放到容器里即可。

        @Override
public void addFormatters(FormatterRegistry registry) {
for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
registry.addConverter(converter);
}
for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
registry.addConverter(converter);
}
for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
registry.addFormatter(formatter);
}
} private <T> Collection<T> getBeansOfType(Class<T> type) {
return this.beanFactory.getBeansOfType(type).values();
}

HttpMessageConverters

  需要自定义HttpMessageConverters,只需要把自己定义的HttpMessageConverters放入容器中,SpringBoot会把HttpMessageConverters添加到SpringMVC中。

    @Bean
public HttpMessageConverters customConverters() {
HttpMessageConverter<?> additional = ...
HttpMessageConverter<?> another = ...
return new HttpMessageConverters(additional, another);
}

SpringBoot--对SpirngMVC的自动配置的更多相关文章

  1. java框架之SpringBoot(5)-SpringMVC的自动配置

    本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...

  2. SpringBoot Beans管理和自动配置

    原 SpringBoot Beans管理和自动配置 火推 02 2017年12月20日 21:37:01 阅读数:220 SpringBoot Beans管理和自动配置 @SpringBootAppl ...

  3. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  4. SpringBoot:配置文件及自动配置原理

    西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...

  5. Springboot学习:SpringMVC自动配置

    Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...

  6. SpringBoot是如何实现自动配置的?--SpringBoot源码(四)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 助力SpringBoot自动配置的条件注解ConditionalOnXXX分析--SpringBoot源码(三 ...

  7. 这一次搞懂SpringBoot核心原理(自动配置、事件驱动、Condition)

    @ 目录 前言 正文 启动原理 事件驱动 自动配置原理 Condition注解原理 总结 前言 SpringBoot是Spring的包装,通过自动配置使得SpringBoot可以做到开箱即用,上手成本 ...

  8. SpringBoot起步依赖和自动配置

    一.起步依赖 1. 是什么 本质上是一个Maven项目对象模型(Project Object Model, POM), 定义了对其他库的传递依赖,这些东西加在一起即支持某项功能. 比如: spring ...

  9. SpringBoot自动化配置之四:SpringBoot 之Starter(自动配置)、Command-line runners

    Spring Boot Starter是在SpringBoot组件中被提出来的一种概念,stackoverflow上面已经有人概括了这个starter是什么东西,想看完整的回答戳这里 Starter ...

  10. springboot(3)——配置文件和自动配置原理详细讲解

    原文地址 目录 概述 1. 配置文件作用 2.配置文件位置 3.配置文件的定义 3.1如果是定义普通变量(数字 字符串 布尔) 3.2如果是定义对象.Map 3.3如果是定义数组 4.配置文件的使用 ...

随机推荐

  1. HTML盒子模型冷知识!!!

    一.边框    1.边框颜色     border-color 边框颜色(可以设置上边框,如:border-top-color,也可以整体设置,但是要注意顺序)     2.边框粗细     bord ...

  2. 「ZJOI2019」语言

    传送门 Description 给定一棵\(n\)个点的树和\(m\)条链,两个点可以联会当且仅当它们同在某一条链上,求可以联会的点的方案数 \(n,m\leq10^5\) Solution  考虑计 ...

  3. 帝国cms7.5整合百度编辑器ueditor教程

    1.根据自己使用的帝国cms版本编码下载对应的ueditor版本 下载地址 http://ueditor.baidu.com/website/download.html#ueditor 2.解压附件, ...

  4. 服务器推送(Server push)技术总结

    1. 短轮询 ajax按一定间隔去请求 2. 长轮询(long Polling) Long Polling的实现很简单,可分为四个过程: 发起Polling发起Polling很简单,只需向服务器发起请 ...

  5. [LeetCode] 141. Linked List Cycle 链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  6. Oracle-关于Oracle.ManagedDataAccess

    今天调用webservice的时候,运行程序后开始报错以下的错误信息 “/”应用程序中的服务器错误. 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项.试图加载格式不正确的 ...

  7. win10锁住鼠标和键盘操作。

    以前做的一个winform自动更新程序没考虑到程序在更新过程中禁止操作被更新程序.现在加上了更新过程中锁住鼠标和大部分键盘. 碰到问题:用系统api -- BlockInput(true)锁住屏幕无效 ...

  8. TCP/IP学习笔记15--IP分割处理与再构成处理,路径MTU发现

           家庭应该是爱.欢乐和笑的殿堂.--日 木村久一        IP属于网络层,下一层时数据链路层,在数据链路层,不同类型的数据链路的最大传输单元(MTU)都不尽相同.例如,连接两个路由器 ...

  9. excel文件导出和导入

    pom.xml添加依赖 @RestController @RequestMapping(value = "/excel") public class ExpImpExcelCont ...

  10. [转帖]keepalived工作原理和配置、使用

    keepalived工作原理和配置.使用 https://www.iteye.com/blog/aoyouzi-2288124 keepalived是什么 keepalived是集群管理中保证集群高可 ...