spring6.0.x源碼調(diào)試環(huán)境搭建

# 效果
搭建一個(gè)spring源碼調(diào)試環(huán)境,創(chuàng)建一個(gè)spring-demo模塊,寫一些測(cè)試代碼。

給源碼添加注釋。

給源碼打包

# ubantu環(huán)境下搭建spring6.0.x源碼環(huán)境
## 步驟
### 源碼網(wǎng)址
[Spring Framework](https://github.com/spring-projects/spring-framework)

### 下載代碼
fork到自己的GitHub倉(cāng)庫(kù),然后拉代碼
```shell
git clone https://github.com/GitHubXiaoSiyuan/spring-framework-6.0.7.git
代碼拉到 ~/files/projects/kernel_projects/fr
amework 目錄下
```

### gradle下載與配置
#### 下載
https://gradle.org/releases/
gradle/wrapper/gradle-wrapper.properties
找到版本為7.6的 gradle
```
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
```

找到 7.6 的版本,點(diǎn)擊下載(注:點(diǎn)擊之后,用迅雷下載很快)
下載鏈接如下(復(fù)制即可觸發(fā)迅雷下載)
```
https://downloads.gradle-dn.com/distributions/gradle-7.6-all.zip
```

#### 解壓
```shell
sudo unzip gradle-7.6-all.zip
```


#### 配置環(huán)境變量
```shell
# 設(shè)置環(huán)境變量
sudo vi /etc/profile
# 在底部加入這一段
# gradle
export GRADLE_HOME=/home/xiaosy/files/development/gradle-7.6?
export PATH=$NODE_HOME/bin:$PATH
# 變量生效
source /etc/profile
# 設(shè)置讀寫權(quán)限
sudo chmod -R 777 /home/xiaosy/files/development/gradle-7.6/bin
# 查看版本
# 不知道為什么直接 gradle -v 沒用
/home/xiaosy/files/development/gradle-7.6/bin/gradle -v
```


### idea配置
#### 配置gradle編譯
Tools -> gradle
```
# 路徑
壓縮包放在 /gradle/wrapper/ 目錄下
路徑配置
/home/xiaosy/files/development/gradle-7.6
```


#### 下載二進(jìn)制版本
```
https://services.gradle.org/distributions/gradle-7.6-bin.zip
```

#### jdk
設(shè)置為 jdk17
```shell
# 設(shè)置環(huán)境變量
sudo vi /etc/profile
# 修改jdk路徑
# jdk
export JAVA_HOME=/home/xiaosy/files/development/jdk17/jdk-17.0.6
# 變量生效
source /etc/profile
```

#### gradle-wrapper.properties修改
打開 gradle/wrapper/gradle-wrapper.properties
將distributionUrld地址替換為本地gradle下載
```
# 修改后
distributionUrl=/home/xiaosy/files/development/gradle-7.6-all.zip
```

#### build.gradle文件修改
替換國(guó)內(nèi)鏡像
```
repositories {
maven { url "https://maven.aliyun.com/repository/central" }
mavenCentral()
maven {
url "https://repo.spring.io/milestone"
content {
// Netty 5 optional support
includeGroup 'io.projectreactor.netty'
}
}
maven { url "https://repo.spring.io/libs-spring-framework-build" }
if (version.contains('-')) {
maven { url "https://repo.spring.io/milestone" }
}
if (version.endsWith('-SNAPSHOT')) {
maven { url "https://repo.spring.io/snapshot" }
}
}
```

#### setting.gradle文件修改
替換國(guó)內(nèi)鏡像
```
repositories {
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url "https://maven.aliyun.com/repository/google"
}
maven { url "https://maven.aliyun.com/repository/gradle-plugin/" }
gradlePluginPortal()
google()
mavenCentral()
}
```

#### 注釋
//注釋掉不然會(huì)A build scan was not published as you have not authenticated with server 'ge.spring.io'.

#### 用idea集成的gradlereload

編譯成功

### 新建module

在新項(xiàng)目的build.gradle下添加對(duì)spring模塊的依賴,這里我先添加了spring-beans 和spring-core的依賴。
```
dependencies {
? ? compile(project(":spring-beans"))
? ? compile(project(":spring-core"))
? ? testCompile group: 'junit', name: 'junit', version: '4.12'
}
```

在新項(xiàng)目的src/main/resource下添加spring-config.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">
<!--把對(duì)象的創(chuàng)建交給spring來管理-->
<bean id="person" class="com.wts.Person">
<property name="id" value="1"></property>
<property name="name" value="zhangsan"></property>
</bean>
</beans>
```
創(chuàng)建測(cè)試bean和啟動(dòng)類
```
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
```
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ac.getBean("person");
System.out.println(person);
}
}
```
## debug
### 1. 更改壓縮包位置
```
The specified Gradle distribution 'file:/home/xiaosy/files/projects/kernel_projects/framework/spring-framework-6.0.7/gradle/wrapper/home/xiaosy/files/development/gradle-7.6-all.zip' does not exist.
```

### 2
設(shè)置文件讀寫權(quán)限
Could not create parent directory for lock file /gradle-7.6/wrapper/dists/gradle-7.6-all/cmg34oui1skho6ogkheeq1oxe/gradle-7.6-all.zip.lck
```
sudo chmod -R 777 ~/files/projects/kernel_projects/framework/spring-framework-6.0.7
```

### 3
Cause: zip file is empty

### 4

解決:
gradle.properties
```
org.gradle.java.home=/home/xiaosy/files/development/jdk17/jdk-17.0.6
```
倆 gradle.properties 都設(shè)置了

注釋掉這段代碼,然后重寫就不報(bào)錯(cuò)了,純粹的編譯問題

#### 參考
1. [Spring 6 源碼編譯和高效閱讀源碼技巧分享](https://blog.51cto.com/u_12386660/5949894),[備份](https://app.yinxiang.com/u/0/client/web#?b=43b768f0-ad67-412d-8d92-93ed2358c674&n=06b896b0-8844-487f-9f6b-ebae35a728fc&s=s59&)
2.?
## 參考
1. [Spring6.0.0源碼閱讀環(huán)境搭建-gradle構(gòu)建編譯](https://blog.csdn.net/m0_37904184/article/details/127945857),[備份](https://app.yinxiang.com/u/0/client/web#?n=2f337807-c141-4572-b386-9d17c060efc1&b=43b768f0-ad67-412d-8d92-93ed2358c674&legacy=p)
2. [Spring源碼深度解析:一、Spring整體架構(gòu)和源碼環(huán)境搭建](https://blog.csdn.net/wts563540/article/details/126686645),[備份](https://app.yinxiang.com/u/0/client/web#?n=08cbcf6c-46aa-40df-90c5-ce4d86a0d040&b=43b768f0-ad67-412d-8d92-93ed2358c674&legacy=p)
3.?