手把手教你如何下载编译Spring源码

145 阅读1分钟

楔子

学习 Spring 源码有两种方式,一种是直接引入 Spring 依赖,读静态源码或者调试;一种是自己编译 Spring 源码。小七觉得如果想要更好的理解 Spring 源码,自己编译 Spring 源码是很有必要的,一方面我们可以修改源码,方便调试验证我们的一些猜想,另一方面也可以写上一些学习的注释,有利于下次阅读复习。

注:本文所说的Spring皆指代spring-framework。

Spring 源码下载

github 下载

github.com/spring-proj…

如果 github 下载缓慢,你还可以尝试国内 gitee 镜像

gitee 下载

gitee.com/mirrors/Spr…

将项目导入开发工具

不同版本的编译要求不同,这里以 idea 为例,小七选择的是 5.2.x。

选择版本为 5.2.x

在这里插入图片描述

修改 IDEA 中 Gradle 配置如下

在这里插入图片描述

确认IDEA中的Kotlin是否生效

在这里插入图片描述

添加阿里云镜像

在根目录的build.gradle中找到 repositories标签,并添加阿里云镜像:

repositories {
   mavenCentral()
   // =====阿里云镜像start=====
   maven { url "https://maven.aliyun.com/nexus/content/groups/public/"}
   maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter"}
   // =====阿里云镜像end=====
   maven { url "https://repo.spring.io/libs-spring-framework-build" }
}

Reload项目

点击右上角重新载入项目 在这里插入图片描述

耐心等待编译完成

在这里插入图片描述

新建Gradle模块

模块名称,无所谓,小七这里取名为:spring-example 在这里插入图片描述

在新模块下添加spring-context依赖

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

编写测试方法

TestConfig

@ComponentScan("com.sheep.config")
public class TestConfig {
}

TestOne

@Component
public class TestOne {
}

Test

public class Test {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext
            context = new AnnotationConfigApplicationContext(TestConfig.class);
      System.out.println("启动成功=======>"+context.getBean(TestOne.class));
   }
}

输出结果

在这里插入图片描述