SpringFramework源码编译
# SpringFramework源码编译
# 前言
前前后后,看过几次Spring的源码,但是过一段时间,许多细节都遗忘了,因此特意在我的github项目中,新建了SpringFramework一个项目spring-framework5.3.10 (opens new window),用来跟进Spring相关的底层源码,以及写一些自己的源码注释
# 一、下载SpringFramework5.3.10源码
我们可以去SpringFramework的releases (opens new window)页面搜索下载相应的版本源码,这里我下载的是Spring-framework-v5.3.10 (opens new window)
# 二、下载Spring源码所需要的依赖
Spring是通过gradle来编译源码下载依赖的,.gradle⽂件夹可以理解为gradle的仓库(和maven类似)。
Spring源码存在很多依赖包,当时引入依赖时,耗费了我许多时间,这里将需要的依赖打个包,方便下次快速处理依赖
链接: https://pan.baidu.com/s/1SiXMe6wvy8qPGJJCYf1Sxg 密码: kjqb
# 三、配置idea相关
# 1、将下载下来的依赖放到自己指定的Gradle user home
中,此处为/Users/ruanyou
,并解压
# 2、修改Build and run using
和 Run tests using
为 IDEA
# 3、注释spring-core
模块中jfr相关的代码,jdk8版本不支持。然后点击build,构建成功就可以了
# 四、运行代码
# 1、新增测试模块 ruanyou-spring-framework-5.3.10
# 2、引入依赖和增加测试类
build.gradle
文件如下:
plugins {
id 'java'
}
group 'org.springframework'
version '5.3.10'
repositories {
mavenCentral()
}
dependencies {
api(project(":spring-context"))
api(project(":spring-beans"))
api(project(":spring-core"))
optional("org.apache.commons:commons-pool2")
optional("com.jamonapi:jamon")
testImplementation(testFixtures(project(":spring-beans")))
testImplementation(testFixtures(project(":spring-core")))
testFixturesImplementation(testFixtures(project(":spring-beans")))
testFixturesImplementation(testFixtures(project(":spring-core")))
}
test {
useJUnitPlatform()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
OrderService.java
import org.springframework.stereotype.Component;
@Component
public class OrderService {
public void test(){
System.out.println("123123");
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
AppConfig.java
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.ruanyou")
public class AppConfig {
}
1
2
3
4
5
6
2
3
4
5
6
Test.java
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
OrderService orderService = applicationContext.getBean("orderService",OrderService.class);
System.out.println(orderService);
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 3、启动测试代码
# 五、常见问题
# 1、程序包jdk.jfr不存在
注释spring-core
模块中jfr相关的代码,jdk8版本不支持。然后点击build,构建成功就可以了