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