tomcat與springmvc 結合 之---第19篇 springmvc 載入.xml檔案的bean 過程
writedby 張豔濤,看springmvc 的原始碼太難了,怎麼辦,
這篇文章主要分析了看透springmvc的第9章結尾的 如何解析xml 名稱空間標籤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 處理器 --> <bean id="NoannaContoller" name="/go2.action" class="com.zyt.NoannaContoller"></bean> <!-- 處理器對映器 根據bean的name進行查詢Handler 將action的url配置在bean的name中 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- 檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> <mvc:annotation-driven/> </beans>
之前真沒注意過,說的名稱空間配置的標籤是就是xmlns, 標籤就是mvc,如上圖的整個紅色空間,
現在的想法還比較初步,現在理解的主要想法是在dispatcherservlet 一定會配置的就是
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param>
那麼一定會解析這個springmvc.xml
這個流程是createWebApplicationContext
進入configureAndRefreshWebApplicationContext
refresh()
進入
主要下面的 新建了一個XmlBeanDefinitionReader 物件
獲取configLocations 本例中只有classpath:springmvc.xml
進入XmlBeanDefinitionReader物件內部
解析成doc物件
新建一個BeanDefinitionDocumentReader documentReader
這裡注意建立createReaderContext過程之中建立了他的成員
解析出來根物件
解析bean定義
找到了 名字為mvc:annotation-driven 的標籤
接著resolve
接著
讀取所有的META-INF/spring.handlers的值,在所有的spring包內都有一個這個檔案,這個引數 有一個classloader 就帶有他讀取jar包路徑
這裡使用類載入器的parent載入器urlloader
上邊的方法是在ClassLoader.getResource()方法,平時所說的載入模式能在這裡看到
其中最上層Launcher$ExtClassLoader,他的parent是null;
下一層是AppClassLoader
再下一層是URLclassloader
磨磨唧唧的讀取了如下屬性
接著resolve函式中按照讀取的配置檔案 新建物件,反射建立
之後進入namespaceHandler.init(); 建立的org.springframework.web.servlet.config.MvcNamespaceHandler物件的initial()方法
進入父類的方法,將parse方法父類的parsers成員內
接著回到
這個方法裡面比較複雜以後再分析