概述
spring mvc 配置了 <mvc:annotation-driver/> 后 ,如果classpath 里面包含jackson 包,则自动注册MappingJackson2HttpMessageConverter,从而支持json 输出。
AnnotationDrivenBeanDefinitionParser
因为annotation-driver是被AnnotationDrivenBeanDefinitionParser解析,查看源码发现如下,
if (jackson2Present) {
RootBeanDefinition jacksonConverterDef = createConverterDefinition(MappingJackson2HttpMessageConverter.class, source);
GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition(source);
jacksonConverterDef.getConstructorArgumentValues().addIndexedArgumentValue(0, jacksonFactoryDef);
messageConverters.add(jacksonConverterDef);
}
而jackson2Present的值为:
private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
其实 AnnotationDrivenBeanDefinitionParser孩子懂帮我们注入了很多,查看parse 方法发现,:
1)注入了
RequestMappingHandlerMapping的
RootBeanDefinition
2)
RequestMappingHandlerAdapter
3)
ExceptionHandlerExceptionResolver
4)还注册几个如下:
registerBeanNameUrlHandlerMapping(parserContext, source); registerHttpRequestHandlerAdapter(parserContext, source); registerSimpleControllerHandlerAdapter(parserContext, source);