这两天准备看看spring的源码,于是从GitHub上下载了spring的源码。
具体的下载步骤就不说,基本都会。这里推荐一个GitHub的镜像网站,GitHub: Where the world builds software · GitHub (fastgit.org),速度比较快,避免了GitHub时不时连不上、速度很慢的问题。
从仓库把代码clone下来后,因为clone的默认分支是main主分支,在编译前记得将代码分支切换到5.3.x。
5.3.x用的Gradle7.2版本构建的,推荐和spring默认的gradle版本一致。
遇见的问题
在编译时,spring-beans中代码报错,报错信息如下:
F:\project\spring-projects\spring-framework\spring-beans\src\main\java\org\springframework\beans\factory\annotation\AutowiredAnnotationBeanPostProcessor.java:542: 错误: 对determineRequiredStatus的引用不明确
return determineRequiredStatus(
^
AutowiredAnnotationBeanPostProcessor 中的方法 determineRequiredStatus(MergedAnnotation<?>) 和 AutowiredAnnotationBeanPostProcessor 中的方法 determineRequiredStatus(AnnotationAttributes) 都匹配
F:\project\spring-projects\spring-framework\spring-beans\src\main\java\org\springframework\beans\factory\annotation\AutowiredAnnotationBeanPostProcessor.java:543: 错误: 不兼容的类型: 推论变量 T 具有不兼容的上限
ann.asMap(mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType())));
^
下限:MergedAnnotation<?>,Map<String,Object>
下限:AnnotationAttributes
其中, T是类型变量:
T扩展已在方法 <T>asMap(Function<MergedAnnotation<?>,T>,Adapt...)中声明的Map<String,Object>
在排查了很久之后,发现是JDK的问题导致的,最初我电脑上JDK版本时15,在试了11、8之后,最终在JDK1.8时编译正常。
在切换JDK版本为8之后,命令行正常编译,但是在idea导入源码后,编译还是有问题,排查后发现时idea的项目编译版本没有改为8,改了之后,编译通过。
切记要使用JDK8。