如何阅读Spring源码

发布于:2025-07-20 ⋅ 阅读:(19) ⋅ 点赞:(0)

如何阅读Spring源码

简介

最近有许多人问我如何阅读Spring源码,那我便在这给出阅读源码的方法,能够保证本地能够让源码能够运行起来。

Spring 源码环境本地编译

Gradle下载地址

通过网盘分享的文件:gradle-6.4.1-all.zip
链接: https://pan.baidu.com/s/1bnBpq6SpyJ3zyFIA204mmQ?pwd=ftgi 提取码: ftgi

Spring源码下载地址

通过网盘分享的文件:spring-framework-master.zip
链接: https://pan.baidu.com/s/1NFjg4MJmYxoEr78ZyMzxMg?pwd=5wrw 提取码: 5wrw

Spring 源码是需要使用 gradle进行编译的,本教程采用 gradle 6.4.1 进行说明; 第二个文件是 spring 5.3 版本的源码。

配置Gradle环境变量

解压gradle-6.4.1-all.zip到任意目录,这些配置一定要根据自己实际解压的目录去配置否则会找不到!!!

配置 GARDLE_HOME

image-20250718224411921

配置path

image-20250718224628187

解压 spring-framework-master

修改 settings.gradle 文件

添加国内阿里云依赖地址:

maven { url 'https://maven.aliyun.com/repository/public' }

image-20250718224847325

修改 gradle.properties 文件

将 org.gradle.jvmargs的值修改为 -Xmx2048M; 并添加 org.gradle.daemon=true

image-20250718224922585

修改 build.gradle 文件

加上使用阿里云仓库,提高下载速度

maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}

image-20250718225006645

修改setting.gradle 删除 第11 行

image-20250718225033305

修改 build.gradle 第29 行

删除 snapshot

image-20250718225105803

修改代码

修改spring-framework-master\buildSrc\src\main\java\org\springframework\build\compile\CompilerConventionsPlugin.java 代码,删除第 62 行的 “-Werror”, 注意把之前的逗号也删掉(或者可以直接用我的覆盖Static代码)

static {
		List<String> commonCompilerArgs = Arrays.asList(
				"-Xlint:serial", "-Xlint:cast", "-Xlint:classfile", "-Xlint:dep-ann",
				"-Xlint:divzero", "-Xlint:empty", "-Xlint:finally", "-Xlint:overrides",
				"-Xlint:path", "-Xlint:processing", "-Xlint:static", "-Xlint:try", "-Xlint:-options"
		);
		COMPILER_ARGS = new ArrayList<>();
		COMPILER_ARGS.addAll(commonCompilerArgs);
		COMPILER_ARGS.addAll(Arrays.asList(
				"-Xlint:varargs", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation",
				"-Xlint:unchecked"
		));
		TEST_COMPILER_ARGS = new ArrayList<>();
		TEST_COMPILER_ARGS.addAll(commonCompilerArgs);
		TEST_COMPILER_ARGS.addAll(Arrays.asList("-Xlint:-varargs", "-Xlint:-fallthrough", "-Xlint:-rawtypes",
				"-Xlint:-deprecation", "-Xlint:-unchecked", "-parameters"));
}

编译

编译 spring-oxm

进入spring源码的位置使用cmd进入控制台,运行以下命令(第一次下载会比较慢):

gradle.bat :spring-oxm:compileTestJava

看到如下则证明依赖下完了

image-20250718225628524

编译spring-core 模块

编译完成后,在执行一下命令:

gradle.bat :spring-core:compileTestJava

看到如下则证明编译完了

image-20250718225819546

将Spring源码导入 idea 并进行配置

验证源码是否可用

在源码目录下新建一个模块

build.gradle中的依赖如下:

plugins {
    id 'java'
}

group = 'org.spring'
version = '5.3.0-SNAPSHOT'

repositories {
    mavenLocal()
    maven{url 'https://maven.aliyun.com/nexus/content/groups/public'}
    maven{url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    mavenCentral()
}

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

}

test {
    useJUnitPlatform()
}
测试

image-20250718230001908

结果

image-20250718230237976

结尾

至此,已经能够保证本地Spring源码是能够使用的了,如有小白有配置上的问题,欢迎后台私信哟,看见会马上回复的。


网站公告

今日签到

点亮在社区的每一天
去签到