A. 怎麼閱讀Spring源碼
准備工作
1. 安裝github:現在spring源代碼都在github管理,所以首先需要下載githup,下;
2. 安裝gradle構建工具: 下載完後進行解壓到任意盤符,然後增加環境變數GRADLE_HOME,並在環境變數bin中增加%GRADLE_HOME%/bin,打開DOS窗口,運行gradle -v,出現版本號等信息,表示安裝成功;
3. 下載Spring源碼:首先打開git shell,切換到你的工作目錄,然後輸入以下命令:git clone git://github.com/SpringSource/Spring-framework.git,後面一串是源碼下載地址。大概半小時的樣子,就可以下載完成,這時候在你的工作目錄中就會出現Spring-framework的目錄,裡面有Spring各組件的源碼包;
4. 構建導入:下載下來的代碼不能直接導入Eclipse,要先轉換成Eclipse能讀取的形式。因為所有組件都會依賴spring-core,所有我們首先要轉換Spring-core工程,在命令窗口切換到Spring-core工程,運行gradle cleanidea eclipse命令,我們會看到開始下載工程所依賴的jar包,幾分鍾後執行完畢,再來看Spring-core文件夾,多了.classpath、.project等文件,這是Eclipse工程所必須的,然後可以把他導入到eclipse。因為大部分Spring組件都會用到 spring-beans、spring-context、spring-aop,而他們又依賴spring-expression、spring-instrument,所以我們乾脆先把這些工程都進行轉換並導入eclipse。
我初次導入過程並不順利,拿spring-core為例,其中以來的一個jar包是Spring-framework/spring-core/build/libs/spring-asm-repack-4.0.jar,但我工程裡面並沒有他,只好在網上下載了一個,並加入構建路徑,其次我還發現少commons-pool-1.5.3.jar、spring-cglib-repack-3.0.jar,都一一下載,最後還是報錯沒有java.util.concurrent.ForkJoinPool類,發現這個版本必須使用jdk1.7以上,1.6沒有這個包。折騰半天,終於幾個工程沒變異錯誤了,向前邁進了一步。
B. spring的源碼jar怎麼使用
首先,在工程右鍵,屬性中,添加必要的jar包。
選中必要的jar包,上面給出的源碼jar包中,導入spring3.0.5中的所有jar包。
其中lib內的是spring的jar包,用到哪個導入哪個,不知道的話,全部導入就行了。
外面的幾個jar包,用於日誌以及mysql的驅動。commons-logging jar包是必須的,其他的隨意吧。
不確定的話,lib外面的這幾個jar包以及lib裡面的都導入就行了。
C. 阿里內部都在用的Spring源碼手冊,學會也是阿里人
最近在使用Spring MVC過程中遇到了一些問題,網上搜索不少帖子後雖然找到了答案和解決方法,但這些答案大部分都只是給了結論,並沒有說明具體原因,感覺總是有點不太滿意。
更重要的是這些所謂的結論大多是抄來抄去,基本源自一家,真實性也有待考證。
那作為程序員怎麼能知其所以然呢?
此處請大家內心默讀三遍。
閱讀源碼的魅力在於:
分享一本阿里內部人都在使用的Spring源碼手冊分享給讀者朋友們,學會掌握了本手冊內容,距離成為阿里人也是成功的跨了一大步子。
第一部分:核心實現原理
第二部分:企業應用
D. 怎麼閱讀spring源碼
從HttpServletBean的init()進入,再到initWebApplicationContext(),再到refresh(),再到refreshBeanFactory(),再到finishRefresh(),直到伺服器啟動成功。不知道讀了多少遍,
但是源碼的東西實在的太多了,想要完全讀懂,完全理清頭緒,還差很遠啊。所以我只重點關注了兩塊內容,就是bean的定位載入解析注冊、bean的實例化兩大塊內容,其他地方稍作了解,沒有太過深入。
整個容器的啟動流程,都在AbstractApplicationContext的refresh()的模板方法中了。
復制代碼
1 public void refresh() throws BeansException, IllegalStateException {
2 synchronized (this.startupShutdownMonitor) {
3 // Prepare this context for refreshing.
4 prepareRefresh();
5
6 // Tell the subclass to refresh the internal bean factory.
7 beanFactory = obtainFreshBeanFactory();
8
9 // Prepare the bean factory for use in this context.
10 prepareBeanFactory(beanFactory);
11
12 try {
13 // Allows post-processing of the bean factory in context subclasses.
14 postProcessBeanFactory(beanFactory);
15
16 // Invoke factory processors registered as beans in the context.
17 (beanFactory);
18
19 // Register bean processors that intercept bean creation.
20 registerBeanPostProcessors(beanFactory);
21
22 // Initialize message source for this context.
23 initMessageSource();
24
25 // Initialize event multicaster for this context.
26 ();
27
28 // Initialize other special beans in specific context subclasses.
29 onRefresh();
30
31 // Check for listener beans and register them.
32 registerListeners();
33
34 // Instantiate all remaining (non-lazy-init) singletons.
35 (beanFactory);
36
37 // Last step: publish corresponding event.
38 finishRefresh();
39 }
40
41 catch (BeansException ex) {
42 // Destroy already created singletons to avoid dangling resources.
43 destroyBeans();
44
45 // Reset 'active' flag.
46 cancelRefresh(ex);
47
48 // Propagate exception to caller.
49 throw ex;
50 }
51 }
52 }
其實,我並沒有上來就看源碼,而是先從看書開始,稍微了解,知道了一些關鍵點,關鍵流程,自己產生了一堆疑問,然後帶著疑問去讀源碼,讀著讀著,發現有些疑問就這么解決了。