【spring源碼分析】準備工做

前言:以前寫過兩篇基於xml形式的IOC容器初始化過程,如今看來寫的比較爛,最近又繼續閱讀spring源碼,對IOC容器的初始化有了一些新的認識,所以決定記錄下來,加深本身對spring的印象與理解。git

注:筆者fork的spring源碼項目地址:https://github.com/morewindows0/spring-frameworkgithub

該項目會不按期更新,因此代碼上可能會有變化。spring

spring版本:5.1.0.BUILD-SNAPSHOT(會按期合併master分支,該版本號會有改變)windows


1.調試源碼準備

從github上下載源碼,編譯成功後,便可進行spring源碼的調試工做(具體如何操做自行搜索解決便可)。因爲咱們只須要對IOC相關內容進行調試,所以在spring-context模塊下操做便可。程序結構以下:測試

在basebean包下建立MultiConditionBean類,並進行相關注入配置。注:採用xml形式進行配置,便於閱讀與理解,註解形式的注入流程與xml形式大體相同。spa

具體代碼以下:3d

xml具體配置內容以下圖:調試

2.建立測試代碼,進行測試

經過以上配置就能夠建立測試代碼進行測試了,測試代碼包結構以下:code

 1     /**
 2      * xml形式注入bean
 3      */
 4     @Test
 5     public void testXmlIOC() {
 6 
 7         System.out.println("xml形式注入bean調試過程開始");
 8         // classpath*:com/dev/config/*
 9         ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:com/dev/config/multicondition.xml");
10 
11         MultiConditionBean multiConditionBean = (MultiConditionBean) context.getBean("multiConditionBean");
12 
13         System.out.println("class name:" + multiConditionBean.getClass().getName());
14 
15         System.out.println("name屬性:" + multiConditionBean.getName());
16         System.out.println("gender屬性:" + multiConditionBean.getGender());
17 
18         System.out.println("xml形式注入bean調試過程結束");
19     }

運行結果以下:xml

結果正確,說明未出現源碼編譯等相關不可知的問題,下面將正式進入IOC容器初始化的過程當中,因爲spring源碼體系很是的龐大,但願本身能儘可能寫清楚,搞懂相關問題,加油!


by Shawn Chen,2018.11.22日,晚

相關文章
相關標籤/搜索