欢迎光临散文网 会员登陆 & 注册

spring6.0.x源码调试环境搭建

2023-04-09 00:48 作者:老剑仙pro  | 我要投稿


# 效果


搭建一个spring源码调试环境,创建一个spring-demo模块,写一些测试代码。



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1680963213000ehfkgc.png)


给源码添加注释。


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1680963429000t29unb.png)



给源码打包


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1680963389000x0la82.png)




# ubantu环境下搭建spring6.0.x源码环境


## 步骤


### 源码网址


[Spring Framework](https://github.com/spring-projects/spring-framework)



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/16799267650003l9kkg.png)



### 下载代码


fork到自己的GitHub仓库,然后拉代码


```shell

git clone https://github.com/GitHubXiaoSiyuan/spring-framework-6.0.7.git


代码拉到 ~/files/projects/kernel_projects/fr

amework 目录下


```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/16799271110005yp0xa.png)



### gradle下载与配置


#### 下载


https://gradle.org/releases/


gradle/wrapper/gradle-wrapper.properties


找到版本为7.6的 gradle

```

distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip

```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679927257000sbqasd.png)



找到 7.6 的版本,点击下载(注:点击之后,用迅雷下载很快)


下载链接如下(复制即可触发迅雷下载)

```

https://downloads.gradle-dn.com/distributions/gradle-7.6-all.zip

```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679927424000fswmw3.png)



#### 解压


```shell

sudo unzip gradle-7.6-all.zip

```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679927947000r5p2g3.png)


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679927983000wjyh33.png)



#### 配置环境变量


```shell

# 设置环境变量

sudo vi /etc/profile


# 在底部加入这一段

# gradle

export GRADLE_HOME=/home/xiaosy/files/development/gradle-7.6 

export PATH=$NODE_HOME/bin:$PATH


# 变量生效

source /etc/profile


# 设置读写权限

sudo chmod -R 777 /home/xiaosy/files/development/gradle-7.6/bin


# 查看版本

# 不知道为什么直接 gradle -v 没用

/home/xiaosy/files/development/gradle-7.6/bin/gradle -v

```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679928189000b7pbyu.png)



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679928401000nvb48a.png)



### idea配置


#### 配置gradle编译


Tools -> gradle


```

# 路径

压缩包放在 /gradle/wrapper/ 目录下


路径配置

/home/xiaosy/files/development/gradle-7.6



```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929506000yfnvjm.png)


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929584000q7r1fh.png)



#### 下载二进制版本


```

https://services.gradle.org/distributions/gradle-7.6-bin.zip

```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679934576000u5ojbg.png)





#### jdk


设置为 jdk17


```shell

# 设置环境变量

sudo vi /etc/profile



# 修改jdk路径

# jdk

export JAVA_HOME=/home/xiaosy/files/development/jdk17/jdk-17.0.6



# 变量生效

source /etc/profile


```



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/167998843000008zoai.png)



#### gradle-wrapper.properties修改


打开 gradle/wrapper/gradle-wrapper.properties


将distributionUrld地址替换为本地gradle下载


```

# 修改后

distributionUrl=/home/xiaosy/files/development/gradle-7.6-all.zip

```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/167992913500046fqby.png)




#### build.gradle文件修改


替换国内镜像



```

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" }

}

}


```



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929274000ahqgno.png)



#### setting.gradle文件修改


替换国内镜像



```

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()

}


```


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929287000a6pt3a.png)



#### 注释


//注释掉不然会A build scan was not published as you have not authenticated with server 'ge.spring.io'.


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679934855000a8g2tu.png)



#### 用idea集成的gradlereload


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929316000glnh3d.png)




编译成功


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679993513000z742ef.png)



### 新建module


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1680022359000ghwzeq.png)



在新项目的build.gradle下添加对spring模块的依赖,这里我先添加了spring-beans 和spring-core的依赖。


```

dependencies {

    compile(project(":spring-beans"))

    compile(project(":spring-core"))

    testCompile group: 'junit', name: 'junit', version: '4.12'

}

```



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/16800225050001wj7rf.png)




在新项目的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">

<!--把对象的创建交给spring来管理-->

<bean id="person" class="com.wts.Person">

<property name="id" value="1"></property>

<property name="name" value="zhangsan"></property>

</bean>

</beans>



```



创建测试bean和启动类


```

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.


```



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929394000x48v4x.png)





### 2


设置文件读写权限


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

```



![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679929679000ku4rli.png)



### 3



Cause: zip file is empty


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679934236000u3uole.png)




### 4


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679989129000l2vfdw.png)


解决:


gradle.properties


```

org.gradle.java.home=/home/xiaosy/files/development/jdk17/jdk-17.0.6

```


俩 gradle.properties 都设置了


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679989274000kh9zm9.png)



注释掉这段代码,然后重写就不报错了,纯粹的编译问题


![gh](https://fastly.jsdelivr.net/gh/GitHubXiaoSiyuan/images@main/2023/1679993478000870uh6.png)



#### 参考


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源码阅读环境搭建-gradle构建编译](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整体架构和源码环境搭建](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. 


spring6.0.x源码调试环境搭建的评论 (共 条)

分享到微博请遵守国家法律