1. 安装gradle
1.1 下载gradle
gradle.org/gradle-down… 下载下来的zip压缩包,解压后,放在自己选择的路径下
1.2 添加环境变量
终端输入
vi ~/.bash_profile
配置环境变量
export GRADLE_HOME="/usr/local/gradle-5.6.4"
export PATH=$PATH:$GRADLE_HOME/bin
使配置生效
source ~/.bash_profile
1.3 测试是否成功
gradle -version
出现如下窗口 即成功
2. 下载spring源码
3. 修改spring源码gradle配置
3.1 build.gradle增加阿里云镜像
// mavenCentral()
// maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://maven.aliyun.com/nexus/content/groups/public/"}
maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter"}
maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url "https://repo.spring.io/snapshot" } // Reactor
maven { url "https://oss.jfrog.org/artifactory/oss-snapshot-local" } // RSocket
3.2 wrapper
路径:spring-framework-5.2.x/gradle/wrapper
将刚才下载gradle zip压缩包放在这个路径下
修改同路径下的gradle-wrapper.properties文件
distributionUrl=/gradle-5.6.4-bin.zip
4. 源码构建
4.1 导入源码
用编译器导入spring源码,等待构建编译,时间比较长,大概半个小时
如果碰到spring-aspects模块的报错,可以把这个模块直接剔除了,它对于后续的源码分析没有任何作用
4.2 调试源码
新建一个module,命名spring-example
在build.gradle修改配置
description = "Spring Exmaple"
apply plugin: "java"
group 'org.springframework'
version '5.2.23.BUILD-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation project(":spring-context")
implementation project(":spring-instrument")
}
test {
useJUnitPlatform()
}
新建一个TestBean的java类,main方法调试
public class Test {
public static void main(String[] args) {
// spring容器的实例化
AnnotationConfigApplicationContext
context = new AnnotationConfigApplicationContext();
// 相当于把TestBean扫描到了spring容器中
context.register(TestBean.class);
// spring容器的初始化
context.refresh();
System.out.println(context.getBean(TestBean.class));
}
}