0%

Spring源码阅读环境搭建

本文记录了 Spring 源码阅读环境的搭建方式

环境准备

  1. Git
  2. JDK8
  3. Gradle
  4. IntelliJ IDEA 2020.1

注意:

  • Spring master分支需要 JDK11,5.2.x 分支 使用 JDK8 即可

Spring源码仓库地址

下载源码

先 fork 到自己的仓库,然后使用 git clone 下载源码。

注意:这里必须使用 git clone 的方式下载,而不是下载 zip 包的方式,否则会构建失败

指定 clone 5.2.x 的代码:

1
git clone -b 5.2.x https://github.com/jaceding/spring-framework.git

导入IDEA

使用 IDEA 打开

等待 IDEA 加载,一般此时 IDEA 会自动构建

导入时配置IDEA Gradle:

构建

测试

在项目右键创建 module

选择 Gradle Java

填写module信息

在 module 中的 build.gradle 中添加配置:

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

效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
plugins {
id 'java'
}

group 'org.springframework'
version '5.2.17.BUILD-SNAPSHOT'

repositories {
mavenCentral()
}

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

创建测试类 Application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

/**
* @author jaceding
* @date 2021/8/7
*/
@ComponentScan("per.jaceding")
public class Application {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
UserComponent userComponent = context.getBean(UserComponent.class);
System.out.println(userComponent.getClass());
}

@Component
public static class UserComponent {
}
}

执行后输出如下:

遇到的问题

问题1

在源码中添加中文注释后,启动出现乱码:

解决办法

添加:

1
-Dfile.encoding=utf-8

最终重启 IDEA

坚持原创技术分享,您的支持将鼓励我继续创作!