效果

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

给源码添加注释。

给源码打包

ubantu环境下搭建spring6.0.x源码环境步骤源码网址

Spring Framework

下载代码

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

git clone https://github.com/GitHubXiaoSiyuan/spring-framework-6.0.7.git代码拉到 ~/files/projects/kernel_projects/framework 目录下

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 的版本,点击下载(注:点击之后,用迅雷下载很快)

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

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

解压

sudo unzip gradle-7.6-all.zip

配置环境变量

# 设置环境变量sudo vi /etc/profile# 在底部加入这一段# gradleexport 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

idea配置配置gradle编译

Tools -> gradle

# 路径压缩包放在 /gradle/wrapper/ 目录下路径配置/home/xiaosy/files/development/gradle-7.6

下载二进制版本

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

jdk

设置为 jdk17

# 设置环境变量sudo vi /etc/profile# 修改jdk路径# jdkexport 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文件修改

替换国内镜像

repositories {maven { url "https://maven.aliyun.com/repository/central" }mavenCentral()maven {url "https://repo.spring.io/milestone"content {// Netty 5 optional supportincludeGroup '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文件修改

替换国内镜像

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

注释

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

用idea集成的gradlereload

编译成功

新建module

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

dependencies {    compile(project(":spring-beans"))    compile(project(":spring-core"))    testCompile group: 'junit', name: 'junit', version: '4.12'}

在新项目的src/main/resource下添加spring-config.xml文件

创建测试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);}}

debug1. 更改压缩包位置

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

设置文件读写权限

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 都设置了

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

参考

  1. Spring 6 源码编译和高效阅读源码技巧分享,备份

参考

  1. Spring6.0.0源码阅读环境搭建-gradle构建编译,备份
  2. Spring源码深度解析:一、Spring整体架构和源码环境搭建,备份