基于Gradle搭建Spring 5.3.13-release源码阅读环境的详细流程
目录
- # 1、安装JDK
- # 2、安装Gradle
- # 3、Spring版本命名规则
- # 4、下载Spring 5.3.13-release源码
- # 5、修改Spring源码中Gradle配置
- # 6、构建Spring源码
- # 7、导入IDEA 点击File --> New --> Project from Existing Sources...
- # 8、创建Spring源码debug调试模块
# 基于Gradle搭建Spring 5.5.13-release源码阅读环境
Spring版本:5.3.13-release
# 1、安装JDK
首先需要保证本地已经安装JDK1.8及以上版本。这里不做过多赘述,自行安装即可。
# 2、安装Gradle
Spring 5.x开始全部都采用Gradle进行编译,构建源码前需要提前安装Gradle。Gradle官网下载地址为:Gradle官网地址。- 我这里使用的版本是最新的
gradle-7.3.1。下载链接为:Gradle-7.3.1下载地址。 - 下载完成之后解压到文件夹。
- 配置
Gradle环境变量,在环境变量的系统变量中添加如下:
$ GRADLE_HOME $ D:\develop\IDE-Gradle\gradle-7.3.1

在系统环境变量的path中添加环境变量:
$ %GRADLE_HOME%\bin

检测环境,使用gradle -v命令在Terminal中查看:
$ gradle -v
显示如下图则代表gradle安装成功。

在gradle安装目录下的init.d文件夹下新建一个init.gradle文件,加入如下配置:
repositories中配置获取依赖jar包的顺序:- 先是从本地
maven仓库中获取 - 然后
mavenLocal()是获取maven本地仓库的路径,和第一条一样,但是不冲突 - 第三条第四条分别为国内
alibaba镜像仓库和国外bstek镜像仓库 - 最后
mavenCentral()是从apache提供的中央仓库中获取依赖jar包
allprojects {
repositories {
maven { url 'file:///D:/develop/IDE-Repository'}
mavenLocal()
maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
maven { name "Bstek" ; url "https://nexus.bsdn.org/content/groups/public/" }
mavenCentral()
}
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/jcenter'}
maven { url 'https://maven.aliyun.com/nexus/content/groups/public'}
}
}
# 3、Spring版本命名规则
| 版本名称 | 版本 | 版本意思 |
|---|---|---|
| snapshot | 快照版 | 尚不稳定,处于开发中的版本 |
| release | 稳定版 | 功能相对稳定的版本,可以对外发行,但是有时间限制 |
| GA(General Availability) | 正式版 | 可广泛使用的稳定版本 |
| M(Milestone) | 里程碑版 | 具有一些全新的功能货时具有里程碑意义的版本 |
| RC(Release Candidate) | 最终测试 | 即将作为正式版本发布的版本 |
# 4、下载Spring 5.3.13-release源码
自Spring 3.x开始,Spring官方不在提供源码下载,所有源代码全部托管在github。Spring Github托管代码首页地址。
spring framework 5.3.13-release:
- 源码访问地址:
https://github.com/spring-projects/spring-framework/releases/tag/v5.3.13 - 源码
zip压缩包下载地址:https://github.com/spring-projects/spring-framework/archive/refs/tags/v5.3.13.zip - 源码
tar.gz压缩包下载地址:https://github.com/spring-projects/spring-framework/archive/refs/tags/v5.3.13.tar.gz - 国内
gitee托管源码访问地址:https://gitee.com/mirrors/Spring-Framework?_from=gitee_search - 国内
getee源码zip压缩包下载地址:https://gitee.com/mirrors/Spring-Framework/repository/archive/v5.3.13
下载完成之后解压即可。
# 5、修改Spring源码中Gradle配置
修改Spring源码的Gradle构建配置,在Spring源码下修改gradle/wrapper/gradle-wrapper.properties文件如下:
修改distributionUrl为本地的gradle安装包路径,从本地拉去,加快源码构建编译的速度。
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists ## distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip ## 配置本地gradle, 配置之后需要配置gradle的中央仓库(阿里云maven中央仓库) distributionUrl=file:///D:/develop/IDE-Gradle/gradle-7.3.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
设置阿里云镜像:
修改根目录下build.gradle文件中的repositories:
repositories {
// 配置本地maven仓库
mavenLocal()
// 配置阿里云maven仓库
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter/" }
// maven中央仓库
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
修改根目录下settings.gradle文件中pluginManagement下的repositories:
pluginManagement {
repositories {
// 配置阿里云 maven 中央仓库
maven { url 'https://maven.aliyun.com/repository/public/' }
gradlePluginPortal()
maven { url 'https://repo.spring.io/plugins-release/' }
}
}
修改根目录下gradle.properties文件,将jvmargs根据自己本机内存重新分配内存大小,我这里分配了10G的内存:
version=5.3.13 org.gradle.jvmargs=-Xmx10240m org.gradle.caching=true org.gradle.parallel=true kotlin.stdlib.default.dependency=false
# 6、构建Spring源码
使用Terminal进入解压后Spring源码所在的文件目录下,预编译spring-oxm模块:
$ ./gradlew :spring-oxm:compileTestJava
如某个jar包没下载成功等,只需要重新执行./gradlew :spring-oxm:compileTestJava再次进行预编译就行了。
构建完成之后修改根目录下setting.gradle文件,注释掉spring-aspects:
.... include "spring-aop" // 移除aspects // include "spring-aspects" include "spring-beans" include "spring-context" ....
# 7、导入IDEA 点击File --> New --> Project from Existing Sources...
然后选择Spring源码所在的目录

点击ok后选择使用gradle导入然后点击Finish
导入之后IDEA会自动进行构建,终止自动构建,此时还需要进行一些设置,Ctrl+Alt+S快捷键打开IDEA设置。
点击File | Settings | Build, Execution, Deployment | Compiler,修改构建时堆内存大小(根据自己的电脑内存自行分配即可,我这里里是分配了5G):

点击File | Settings | Build, Execution, Deployment | Build Tools | Gradle,配置IDEA的Gradle配置:

Ctrl+Alt+Shift+S快捷键打开Project Structure。修改工程的SDK,分别将Project、Modules、SDKs中的JDK设置为本地安装的JDK(版本为1.8及以上)。
Ctrl+Alt+Shift+S快捷键打开Project Structure。在Modules中排除spring-aspects:

Alt+F12快捷键打开Terminal,使用gradlew.bat命令进行编译
$ gradlew.bat
编译成功后会出现BUILD SUCCESSFUL的提示。如果有报错根据报错信息进行处理,多编译几次即可。
编译成功后整个工程如下图所示:

使用shift+shift快捷键输入ApplicationContext类,使用Ctrl+Shift+Alt+U如果能够成功打开类图,也证明Spring源码构建成功:

# 8、创建Spring源码debug调试模块
使用gradle创建一新的debug模块。
创建完成之后将新建模块的build.gradle修改为:新建的模块名称。如我新建的模块叫spring-context-debug,则将其修改为:spring-context-debug.gradle并在其中配置:
plugins {
id 'java'
}
group 'org.springframework'
version '5.3.13'
repositories {
// 配置本地 maven 仓库
mavenLocal()
// 配置阿里云 maven 仓库
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' }
// 配置 maven 中央仓库
mavenCentral()
dependencies {
// 测试需要依赖
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
// 导入 spring-context模块, 包含bean工厂
implementation(project(':spring-context'))
// 导入 spring-instrument 模块, 此模块为 spring-context 模块编译所必须的
implementation(project('::spring-instrument'))
// compile已经被gradle 7.x 弃用, 爷也是醉了
// compile(project(":spring-context"))
test {
useJUnitPlatform()
为新建模块添加spring-context和spring-instrument依赖。配置完成之后刷新gradle。如下图所示,gradle中新建模块的runtimeClasspath中已经新增project spring-conetxt和project spring-instrument代表以来成功。

创建一个TestBean实现DebugBean
DebugBean.java:
package com.kapcb.ccc.model;
/**
* <a>Title: Bean </a>
* <a>Author: Kapcb <a>
* <a>Description: Bean <a>
*
* @author Kapcb
* @version 1.0
* @date 2021/12/11 23:21
* @since 1.0
*/
public interface DebugBean {
/**
* say method
*/
void say();
}
TestBean.java:
package com.kapcb.ccc.model;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* <a>Title: TestBean </a>
* <a>Author: Kapcb <a>
* <a>Description: TestBean <a>
*
* @author Kapcb
* @version 1.0
* @date 2021/12/11 23:21
* @since 1.0
*/
public class TestBean implements DebugBean {
protected final Log log = LogFactory.getLog(getClass());
private String username;
public TestBean() {
log.info("调用TestBean无参构造器");
}
@Override
public void say() {
log.info("Hi, I'm " + this.username);
public void setUsername(String username) {
log.info("调用TestBean中的setUsername方法注入属性");
this.username = username;
}
添加测试代码,使用注解模式声明一个Bean:
package com.kapcb.ccc.configuration;
import com.kapcb.ccc.model.DebugBean;
import com.kapcb.ccc.model.TestBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* <a>Title: AnnotationTestConfiguration </a>
* <a>Author: Kapcb <a>
* <a>Description: AnnotationTestConfiguration <a>
*
* @author Kapcb
* @date 2021/12/15 16:06
*/
@Configuration
public class AnnotationTestConfiguration {
@Bean("testBean")
@Scope("singleton")
public DebugBean debugBean() {
TestBean testBean = new TestBean();
testBean.setUsername("Kapcb(Annotation)");
return testBean;
}
}
添加测试代码:
package com.kapcb.ccc;
import com.kapcb.ccc.configuration.AnnotationTestConfiguration;
import com.kapcb.ccc.model.DebugBean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* <a>Title: AnnotationApplication </a>
* <a>Author: Kapcb <a>
* <a>Description: AnnotationApplication <a>
*
* @author Kapcb
* @date 2021/12/15 16:05
*/
public class AnnotationApplication {
public static void main(String[] args) {
BeanFactory beanFactory = new AnnotationConfigApplicationContext(AnnotationTestConfiguration.class);
DebugBean testBean = beanFactory.getBean("testBean", DebugBean.class);
testBean.say();
}
}
使用debug模式运行,如果debug模式启动main方法报错,尝试修改:File | Settings | Build, Execution, Deployment | Debugger | Data Views | Kotlin在Disable coroutine agent前勾选后保存。
启动成功后控制台会输出如下日志:
Connected to the target VM, address: '127.0.0.1:55594', transport: 'socket'
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean <init>
信息: 调用TestBean无参构造器
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean setUsername
信息: 调用TestBean中的setUsername方法注入属性
一月 06, 2022 2:56:42 下午 com.kapcb.ccc.model.TestBean say
信息: Hi, I'm Kapcb(Annotation)
Disconnected from the target VM, address: '127.0.0.1:55594', transport: 'socket'
GitHub源码地址:https://github.com/kapbc/kapcb-spring-source/tree/master/Spring-Framework-v5.3.13
到此这篇关于基于Gradle搭建Spring 5.3.13-release源码阅读环境的文章就介绍到这了,更多相关Gradle搭建Spring源码阅读环境内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
