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);
}

最新文章

  1. linux第二天
  2. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备三)
  3. 在此为LCT开一个永久的坑
  4. sqlalchemy 的 ORM 方式使用示例
  5. javascript实现二分查找
  6. Eclipse 下载与安装(2014.12.26——by小赞)
  7. js 连续赋值
  8. 企业门户(Portal)项目实施方略与开发指南
  9. 不定义JQuery插件,不要说会JQuery[转载]
  10. 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
  11. 【Web优化】Yslow优化法则(四)启用Gzip压缩
  12. ASP.NET Web API框架揭秘:路由系统的几个核心类型
  13. IIS 7如何实现http重定向https
  14. nginx常用配置系列-HTTPS配置
  15. html5 input type=&quot;color&quot;边框伪类效果
  16. 《Java项目中classpath路径详解》
  17. git 不区分文件大小写的处理
  18. svn中的ignore
  19. python3.x:No matching distribution found for PIL
  20. 如何使用PhoneGap打包Web App

热门文章

  1. js获取url中的参数,并保证获取到的参数不乱码
  2. Element源码---初识框架
  3. Java两个数的和
  4. Kubernetes kubectl 命令概述
  5. 实验与作业(Python)-03 Python程序实例解析(函数、循环、range、turtle)
  6. linux core 性能
  7. mysql 常用字符串操作
  8. Java_jdbc 基础笔记之十四 数据库连接(元数据)数据库信息及连接信息
  9. 常见的SQL优化面试题
  10. 【Java】单点登录(SSO)