我使用mac電腦,編譯器是idea桌硫,jdk1.8, gradle 4.9
一夭咬、下載源碼
源碼的GitHub地址:https://github.com/spring-projects/spring-framework
可以使用git下載,也可以直接下載zip包铆隘,只不過多了一個(gè)解壓的過程卓舵,我git下載不下來,我直接下載的zip包膀钠,放在我的idea工作文件目錄下:~/IdeaProjects
,使用命令unzip spring-framework-master.zip
掏湾,我想這步都還是比較簡單沒什么問題。
二肿嘲、下載gradle
如果使用 brew install gradle
下載gradle融击,會(huì)下載最新的gradle,目前已經(jīng)是5.5.1的版本雳窟,但是spring源碼是需要的4開頭的版本尊浪,我在使用5.5.1版本編譯的時(shí)候就遇到了報(bào)錯(cuò)This version of Gradle requires version 2.0.2 of the build scan plugin or later.
這表示當(dāng)前版本太高了,下圖可以檢查封救,提示對(duì)應(yīng)的版本拇涤。
三、根據(jù)源碼的提示預(yù)編譯源碼
可以從截圖中看到誉结,源碼里面有相應(yīng)的導(dǎo)入源碼說明鹅士,我是導(dǎo)入到idea中,所以根據(jù)
import-into-idea.md
提示進(jìn)行,可以看到惩坑,分為4步掉盅,但是實(shí)際操作,我發(fā)現(xiàn)第一步還需要增加一點(diǎn)以舒。首先按照文檔趾痘,到spring-framework-5.0.x目錄下,執(zhí)行
./gradlew :spring-oxm:compileTestJava
,當(dāng)然第一次一般都是失敗的蔓钟,我多試了幾次就成功了(多是幾次==我花了兩天下班時(shí)間)永票,希望大家有耐心,然后在spring-framework-5.0.x目錄下還要執(zhí)行./gradlew build -x test
,這個(gè)是因?yàn)槲液竺鏈y試的時(shí)候奋刽,明明寫好了xml文件瓦侮,可是就是報(bào)錯(cuò)org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'teacher' available
,雖然不知道有什么聯(lián)系艰赞,但是沒它不行佣谐,這個(gè)命令的執(zhí)行也是多試幾次,一定要有耐心方妖。這步完成后狭魂,按照文檔提示,導(dǎo)入文件
File -> New -> Project from Existing Sources
,選中你的spring源碼目錄,選中build.gradle
文件雌澄。接下來斋泄,重點(diǎn)來了
圖中1,2镐牺,3炫掐,4大家都這么做,圖中5的地方大家看其他教程可能發(fā)現(xiàn)跟這個(gè)有所不同睬涧,這個(gè)是因?yàn)槲沂褂玫淖约合螺d安裝的gradle募胃,所以是這個(gè)路徑,如果使用的
brew
安裝的gradle,那么路徑應(yīng)該是/usr/local/Cellar/gradle-5.5.1/libexe
畦浓。漫長的等待,long long long time痹束。
中間你可能遇到報(bào)錯(cuò):
No such property: values for class: org.gradle.api.internal.tasks.DefaultTas
,你點(diǎn)擊open file注釋掉最后三行讶请,
接下來就是官方文檔常說的spring-aspects模塊的問題祷嘶,可能遇到,可能遇不到夺溢,不過最好都執(zhí)行一下论巍,選中idea中的spring-framework-5.0.x右鍵 -> Load/Unload modules,將spring-aspects模塊移走,然后rebuild一下企垦。
最后檢查你的源碼項(xiàng)目环壤,是否在有點(diǎn)文件中,有的類或者包為紅色(無法引入)
這個(gè)時(shí)候你可以先到無法引入的類的所在模塊钞诡,執(zhí)行命令
gradle build
如果失敗類郑现,那么使用plan B:
四、添加一個(gè)測試模塊
我一開始想跟spring統(tǒng)一荧降,使用gradle建立測試模塊接箫,我發(fā)現(xiàn)我好像不怎么擅長gradle,而且遇到各種問題朵诫,為了解決這些問題辛友,我最后使用擅長的maven。
首先選中idea中的spring-framework-5.0.x右鍵 -> module -> maven,然后就是取名字剪返,next到最后废累。這是我的測試項(xiàng)目的結(jié)構(gòu)截圖。
還要引入相關(guān)的spring的模塊:file -> project structure
為什么引入這么多模塊脱盲,得往下看邑滨。
然后是代碼:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.laoye.spring</groupId>
<artifactId>my-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Student:
package com.laoye.spring.beans;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Main是空文件,然后測試類
BeansTest:
package com.laoye.test;
import com.laoye.spring.beans.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeansTest {
@Test
public void testStudent(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:application.xml");
Student student = context.getBean("student",Student.class);
System.out.println(student.getName());
}
}
最后是application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.laoye.spring.beans.Student">
<property name="name" value="laoye"/>
<property name="age" value="1"/>
</bean>
</beans>
一開始我只引入了spring-context,spring-beans,spring-core模塊钱反, 然后執(zhí)行測試代碼掖看,會(huì)發(fā)現(xiàn)我一個(gè)問題(解決了好久)
Error:(26, 38) java: 找不到符號(hào)
符號(hào): 類 InstrumentationSavingAgent
位置: 程序包 org.springframework.instrument
當(dāng)時(shí)沒找到什么有效的解決辦法匣距,網(wǎng)上都是讓重新導(dǎo)入項(xiàng)目,實(shí)際解決辦法是:
找個(gè)這個(gè)類所在模塊哎壳,然后添加到測試模塊的依賴中毅待,類InstrumentationSavingAgent
在模塊spring-instrument
中,所以就添加這個(gè)模塊归榕。
然后在此debug測試用例尸红,會(huì)遇到類似的問題,然后就是添加模塊刹泄,我最后添加的模塊很多驶乾,如上圖所示。
五循签、總結(jié)
路漫漫其修遠(yuǎn)兮级乐,吾將上下而求索!
遇到問題县匠,查找問題风科,最后解決問題,這是我們干這一行最基本的方式乞旦,希望更多的人能在程序員的道路上堅(jiān)持下去贼穆,也希望有人能一起討論學(xué)習(xí)。
終于邁出了艱難第一步兰粉。