Spring源码阅读环境的搭建

本着学习Spring源码的心态,但是其中还是出了很多问题。

1. Spring源码下载指南

Spring源码地址:https://github.com/spring-projects/spring-framework

Spring源码构建官方文档:https://github.com/spring-projects/spring-framework/wiki/Build-from-Source

1.1 开始之前

你的电脑需要安装Git,JDK 8 update 262 or later.由于依赖Java Flight Recorder(JFR)类型,因此需要更新262或更高版本。但是请注意,Oracle JDK不包括JFR类型。 因此,我们建议包含JFR类型的AdoptOpenJDK发行版或类似发行版。 确保您的JAVA_HOME环境变量指向从JDK下载中提取的jdk1.8.0文件夹。

1.2 获取源码

直接通过git clone 下载

git clone [email protected]:spring-projects/spring-framework.git

这里最好不要下载指定分支,tag的压缩包,类似这样

image.png

如果你说这样能不能构建,我的回答是可以的,但是可能会走一些弯路。

zip 发行版主要是用来共享源代码,但不一定用于构建它。

1.3 导入IDEA

在将源码下载之后,先不要急着导入IDEA,在项目根目录下,呼出CMD窗口,进行执行

./gradlew build

当提示SUCCESS之后,导入IDEA即可。

当导入IDEA之后,可以新建一个模块,尝试执行简单代码。

2. 报错的解决办法

2.1 Process 'command 'C:/Program Files/AdoptOpenJDK/jdk-8.0.282.8-hotspot/bin/java.exe'' finished with non-zero exit value 1

Execution failed for task ':runnable:SpringTest.main()'.
> Process 'command 'C:/Program Files/AdoptOpenJDK/jdk-8.0.282.8-hotspot/bin/java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

这个并不是环境没有搭建成功,而是代码的问题,先通过简单的sout看看能不能执行。

2.2 jdk.jfr.Category没找到

报错图片

image.png

尝试更换JDK版本,我测试了JDK8以上都可以。比如JDK15,JDK14,jdk-8.0.282.8-hotspot。

安装了新的JDK之后,在IDEA的project structure记得进行更换

image.png

image.png

2.3 gradle构建Spring的时候提示主机连接不上

关闭电脑的个人热点。

image.png

2.4 报错提示乱码信息

类似这样

D:~\CustomAopBeanPostProcessor.java:11: ����: ����GBK�IJ���ӳ���ַ�
//		这里�?有bean都会处理,可以�?�过条件过滤指定bean处理
  		    ^

因为Spring项目的编码都是GBK编码,所以建议删除中文,然后重新运行。

已经尝试过强转convert,但是没有效果,直接删除肯定没问题。

2.5 A build scan was not published as you have not authenticated with server 'ge.spring.io'.

Build scan background action failed.
org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128
	at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:414)
	at org.gradle.process.internal.DefaultExecAction.execute(DefaultExecAction.java:38)
	at org.gradle.process.internal.DefaultExecActionFactory.exec(DefaultExecActionFactory.java:175)
	at io.spring.ge.conventions.gradle.WorkingDirectoryProcessOperations.exec(WorkingDirectoryProcessOperations.java:45)
	at io.spring.ge.conventions.gradle.ProcessOperationsProcessRunner.run(ProcessOperationsProcessRunner.java:41)
	at io.spring.ge.conventions.core.BuildScanConventions.run(BuildScanConventions.java:166)
	at io.spring.ge.conventions.core.BuildScanConventions.addGitMetadata(BuildScanConventions.java:113)
	at io.spring.ge.conventions.gradle.GradleConfigurableBuildScan.lambda$background$0(GradleConfigurableBuildScan.java:104)
	at com.gradle.enterprise.gradleplugin.internal.extension.DefaultBuildScanExtension$3.run(SourceFile:100)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

A build scan was not published as you have not authenticated with server 'ge.spring.io'.

这个注释掉setting.gradle中的ge.spring.io就好了

image.png

3. 终极解决办法

我把我现在构建好的这个直接放在git上,供大家拉取下载。

地址:https://github.com/MingGH/spring-framework

# Spring 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×