【一】映射基本过程
(1)springMVC配置映射,需要在xml配置文件中配置<mvc:annotation-driven > </mvc:annotation-driven>
(2)配置后,该配置将会交由org.springframework.web.servlet.config.MvcNamespaceHandler处理,该类会转交给org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser做解析。
(3)AnnotationDrivenBeanDefinitionParser做解析后,会向IOC容器中注册多个对象。其中有这个对象org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
(4)RequestMappingHandlerMapping对象在IOC实例化bean阶段,会调用该对象afterPropertiesSet()方法做url和bean的映射。
(5)该映射是从IOC中找到标注有@Controller,@RequestMapping注解的bean,然后反射所有的方法Mehod对象和注解中配置的url值进行映射。
(6)在org.springframework.web.servlet.DispatcherServlet初始化调用init()方法,在IOC解析,aop编制好后,会调用initStrategies(ApplicationContext context) 方法里调用initHandlerMappings(context)中。将IOC中所有实现HandlerMapping接口的bean注入到dispatcherServlet属性List<HandlerMapping> handlerMappings中
【二】<mvc:annotation-driven > </mvc:annotation-driven>配置,由AnnotationDrivenBeanDefinitionParser解析会在IOC中注册的bean大致如下
【1】org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
属性:
org.springframework.web.accept.ContentNegotiationManagerFactoryBean
【2】org.springframework.web.bind.support.ConfigurableWebBindingInitializer
属性:
org.springframework.format.support.FormattingConversionServiceFactoryBean
org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean
【3】org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
属性:
contentNegotiationManager:org.springframework.web.accept.ContentNegotiationManagerFactoryBean
webBindingInitializer: org.springframework.web.bind.support.ConfigurableWebBindingInitializer
messageConverters : 下面
【4】<mvc:message-converters register-defaults="true">
==>org.springframework.http.converter.ByteArrayHttpMessageConverter(默认加载)
==>org.springframework.http.converter.StringHttpMessageConverter(默认加载)
==>com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter(配置)
==>org.springframework.http.converter.ResourceHttpMessageConverter(默认加载)
==>org.springframework.http.converter.xml.SourceHttpMessageConverter(默认加载)
==>org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter(默认加载)
==> org.springframework.http.converter.feed.AtomFeedHttpMessageConverter(不加载)
==>org.springframework.http.converter.feed.RssChannelHttpMessageConverter(不加载)
==>org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter(不加载)
==>org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter(默认加载)
==>org.springframework.http.converter.json.GsonHttpMessageConverter(不加载)
【5】org.springframework.web.servlet.config.CompositeUriComponentsContributorFactoryBean
属性:
handlerAdapter:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
conversionService:org.springframework.format.support.FormattingConversionServiceFactoryBean
【6】org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor
【7】org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
【8】org.springframework.web.servlet.handler.MappedInterceptor
【9】org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver
【10】org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
【11】org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
【12】org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
【13】org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter