Spring源码剖析(一):编译 spring 源码(2021 最新版)

版本说明:

  • JDK:11
  • Springframework:5.1.X
  • Gradle:6.5.1

1. 项目克隆

# github 版
git clone -b 5.1.x https://github.com/Xudongenen/spring-framework.git
# 国内镜像(本文采用的)
git clone -b 5.1.x https://gitee.com/mirrors/Spring-Framework.git

2. 下载gradle

 https://services.gradle.org/distributions/gradle-6.5.1-bin.zip

3. 更改spring在项目中gradle下载位置

# 改动文件地址 Spring-Frameworkgradlewrappergradle-wrapper.properties
# 将其中gradle下载地址改为我们下好的目录,他就会不去下载直接用了,如下图所示:

更改获取位置

4. 阿里云配置

修改bulid.gradle文件, 注意加在了哪,可以用“ mavenCentral()”定位一下

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/libs-spring-framework-build" }
    maven { url "https://repo.spring.io/snapshot" }
    maven { url 'https://maven.aliyun.com/nexus/content/groups/public/'}
    maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
  }

阿里源

5. 命令行运行 gradlew.bat

成功的结果如下:

成功运行

失败结果如下:
失败运行

我当时是报了如下错误:
错误信息: Could not initialize class org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSetKt
解决方案: 在 Spring-Frameworkbuild.gradle 中修改kotlin版本,共两处:

# 第一处:
plugins {
  id "io.spring.dependency-management" version "1.0.7.RELEASE" apply false
  id "org.jetbrains.kotlin.jvm" version "1.3.61" apply false //这里!!!!
  id "org.jetbrains.dokka" version "0.9.18"
  id "org.asciidoctor.convert" version "1.5.8"
}
# 第二处:
kotlinVersion        = "1.3.61"  //这里!!!!

6. 导入idea预编译

# 命令行执行
gradlew :spring-oxm:compileTestJava

成功之后如图:
成功运行

当然了事情并不是一帆风顺,我出现了以下错误:

错误信息:
org.gradle.api.CircularReferenceException: Circular dependency between the following tasks:
:spring-beans:compileGroovy
— :spring-beans:compileJava
— :spring-beans:compileKotlin
— :spring-beans:compileGroovy (*)**

解决方案: 更改 Spring-Frameworkspring-beansspring-beans.gradle 文件,方法如下:

  1. 在文件最后注释掉下面三行代码
// def deps = compileGroovy.taskDependencies.immutableValues + compileGroovy.taskDependencies.mutableValues
// compileGroovy.dependsOn = deps - "compileJava"
// compileKotlin.dependsOn(compileGroovy)
  1. 添加如下代码
tasks.named('compileGroovy') {
    // Groovy only needs the declared dependencies (and not the result of Java compilation)
    classpath = sourceSets.main.compileClasspath
}
tasks.named('compileKotlin') {
    // Kotlin also depends on the result of Groovy compilation
    classpath += files(sourceSets.main.groovy.classesDirectory)
}

7. 正式导入idea(要导好一会儿)

导入idea

8. 改用idea构建,主要为了提速

idea构建

9. 新建测试模块

  • 新建模块
    新建模块

  • 选择gradle构建
    新建模块

  • 起名字
    新建模块

10. 为自己的模块添加spring依赖

找到我们自己建的模块中的gradle.bulid
添加依赖

加入如下代码:

compile(project(":spring-context")) 

添加依赖

11. 创建自己的测试类

我的文件目录是这样的:
目录结构

其中涉及到的三个文件如下:

// MyConfig.java 文件
package com.zxd;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.zxd")
public class MyConfig {}

// MyService.java 文件
package com.zxd;
import org.springframework.stereotype.Component;
@Componentpublic 
class MyService {}

// MyTest.java 文件
import com.zxd.MyConfig;
import com.zxd.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {      
    public static void main(String[] args) {            
        AnnotationConfigApplicationContext annotationConfigApplicationContext = 
                                                                new AnnotationConfigApplicationContext (MyConfig.class);            
        System.out.println(annotationConfigApplicationContext.getBean(MyService.class));      
    }
}

运行MyTest类成功便会得到如下结果:
测试成功

但事情往往不会那么顺利,我就遇到了以下两个问题:

错误信息1: Kotlin: Language version 1.1 is no longer supported; please, use version 1.2 or greater.
Errors occurred while compiling module ‘spring.spring-beans.main’

解决方案: 我们需要找到对应报错模块,改变他的Kotlin版本,在这里我改成了 1.4 稳定版,遇到类似问题也是同样的解决办法,如图所示。
错误1

错误信息2:
D:javaSpring-Frameworkspring-contextsrcmainjavaorgspringframeworkcontextweavingDefaultContextLoadTimeWeaver.java:26:38
java: 找不到符号
符号: 类 InstrumentationSavingAgent
位置: 程序包 org.springframework.instrument

解决方案: 通过查看报错信息,我们知道了,编译器没有找到对应的InstrumentationSavingAgent类,我们到这个类所在的模块 spring-instrument 的 out 文件中发现没有这个类,所以我们需要通过手动运行test类将它们编译出来,具体如图所示。
错误二

总结:

终于,我们可以运行spring源码了,过程不难,遇到问题不要慌,版本问题一定要细致,如果是第一次装最好先和我的版本保持一致,轻车熟路之后再换呗~
大家如果有啥问题可以留言哦,没问题我们就一起开始学源码了,大家加油啊!!!

参考:

Build from Source

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>