天天看點

spring及spring mvc源碼透徹解析

這幾天一直在研究spring的源碼,現将研究過程和成果記錄一下,以便以後檢視。

一、從spring加載的入口

二、閱讀《spring源碼深入解析》一書,并結合部落格 【Spring源碼分析】Bean加載流程概覽 及這篇部落格的後續幾篇,再根蹤spring的源碼,了解了spring初始化beanFactory,加載并解析bean配置為BeanDefinition,後又執行個體化bean的過程

三、閱讀《spring源碼深入解析》一書,再根據springmvc的源碼,了解了springmvc xml配置的執行過程

四、【Spring實戰】----Spring配置檔案的解析 及【Spring實戰】Spring注解配置工作原理源碼解析還有其他的關于注解的基本知識,大概了解了spring的注解。

spring啟動component-scan類掃描加載過程---源碼分析 中了解了在執行個體化bean時調用了DefaultBeanDefinitionDocumentReader類的registerBeanDefinitions方法,其調用的delegate.parseCustomElement(ele)方法,會調用NamespaceHandlerSupport的parse方法,它會根據節點的類型,找到一種合适的解析BeanDefinitionParser(如mvc:annotation-driven對應的AnnotationDrivenBeanDefinitionParser)對标簽的内容進行執行個體化。

要查找某個标簽的parser就根據就選擇NamespaceHandlerSupport使用ctrl+H,從其子類中找到标簽對應的Handler,在從Handler中找到對應的parser,如mvc:annotation-driven,就找MvcNamespaceHandler,可以看到annotation-driven對應的中AnnotationDrivenBeanDefinitionParser。

五、SpringMVC源碼之參數解析綁定原理 、 SpringMVC源碼閱讀:Controller中參數解析了解了springmvc參數解析的流程。

RequestMappingHandlerAdapter解析參數需要的解析器都是在afterPropertiesSet方法中初始化的,是以了解了afterPropertiesSet()是在bean執行個體化後執行的,doCreateBean()中initializeBean()的invokeInitMethods()中,會調用bean的afterPropertiesSet()方法。

<mvc:annotation-driven
		ignore-default-model-on-redirect="true">
		<mvc:message-converters register-defaults="true">
			<bean id="mappingJacksonHttpMessageConverter"
				  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=UTF-8</value>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>
				<property name="objectMapper">
					<bean class="com.tqmall.web.converter.CustomObjectMapper" />
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
           

SpringMVC關于json、xml自動轉換的原理研究[附帶源碼分析] 上面MappingJackson2HttpMessageConverter解析器的加載過程,annotation-driven對應的解析器AnnotationDrivenBeanDefinitionParser的parse()方法,在加載RequestMappingHandlerAdapter之前會解析标簽内的message-converters的内容,并賦給RequestMappingHandlerAdapter,在後續解析參數時會用到。

六、結語

目前還沒搞清楚NamespaceHandler的加載流程,後續會再添加上aop其spring事務的源碼分析