SpringBoot--对SpirngMVC的自动配置
SpringBoot对SpringMVC提供了许多自动配置
- Inclusion of
ContentNegotiatingViewResolver
andBeanNameViewResolver
beans. - Support for serving static resources, including support for WebJars (covered later in this document)).
- Automatic registration of
Converter
,GenericConverter
, andFormatter
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);
}
最新文章
- linux第二天
- Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备三)
- 在此为LCT开一个永久的坑
- sqlalchemy 的 ORM 方式使用示例
- javascript实现二分查找
- Eclipse 下载与安装(2014.12.26——by小赞)
- js 连续赋值
- 企业门户(Portal)项目实施方略与开发指南
- 不定义JQuery插件,不要说会JQuery[转载]
- 详解 CSS 属性 - 伪类和伪元素的区别(再也不用概念盲了!!!)
- 【Web优化】Yslow优化法则(四)启用Gzip压缩
- ASP.NET Web API框架揭秘:路由系统的几个核心类型
- IIS 7如何实现http重定向https
- nginx常用配置系列-HTTPS配置
- html5 input type=";color";边框伪类效果
- 《Java项目中classpath路径详解》
- git 不区分文件大小写的处理
- svn中的ignore
- python3.x:No matching distribution found for PIL
- 如何使用PhoneGap打包Web App