本文將粗略的搭建一個Spring源碼的閱讀環境,爲後面的源碼閱讀做一個準備。做任何事情不管是有一個完美的或者是不太完美的開頭,只要去做了,那麼就是一種勝利。
由於spring使用了gradle構建工具,接下來先安裝gradle。
gradle -v
,能看到gradle的版本信息表示安裝已經成功Spring在github上的倉庫地址是:https://github.com/spring-projects/spring-framework,本文不會直接去github上去下載源碼,網速實在太慢。本文使用的碼雲上Spring倉庫的鏡像,該鏡像每日同步一次,地址是https://gitee.com/mirrors/Spring-Framework
從gradle導入Spring項目,等待gradle build完成
注意:
.gradle
目錄,對於Administrator
用戶,對應的目錄是C:\Users\Administrator\.gradle
。該目錄佔用的空間一般比較多,所以在這裏將這個目錄放到其他的盤中。構建完成後報錯如下(只列出了一部分):
Copy
... Error:(63, 30) java: cannot find symbol symbol: class Signature location: class org.springframework.cglib.core.KeyFactory ... location: class org.springframework.cglib.proxy.Enhancer Error:(152, 30) java: cannot find symbol ...
spring未了避免與cglib和objenesis衝突,將cglib和objenesis相關的包重新repack到org.springframework.cglib
和org.springframework.objenesis
包中,這部分的代碼沒有包含到源碼當中。構建之前,可以通過添加Gradle任務來解決,見:https://github.com/spring-projects/spring-framework/blob/master/import-into-idea.md#known-issues和https://youtrack.jetbrains.com/issue/IDEA-160605
解決辦法如下:
爲了方便編寫測試spring的代碼,在spring-framework單獨新建一個模塊my-test
一路下一步,最後點擊完成,新的模塊就建好了
在my-test模塊中編寫程序測試
創建MyApplication
Copy
package com.zfx; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApplication { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Hello hello = (Hello)ac.getBean("hello"); hello.sayHello(); } }
在resources目錄下新建applicationContext.xml
Copy
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.zfx.Hello"></bean> </beans>
新建Hello
類
Copy
package com.zfx; public class Hello { public void sayHello() { System.out.println("Hello, zhangfengxian"); } }
運行MyApplication
,可以看到控制檯輸出:Hello, zhangfengxian
至此整個環境算是搭建好了
spring-aspects
模塊構建時報錯#解決辦法一:排除spring-aspects
模塊
在工具欄點擊File -> Project Structure...
解決辦法二:使用Acj或者Eclipse編譯
在工具欄點擊File -> Settings...
此問題的相關鏈接: