如何搭建Spring源码阅读项目,基于5.2.x版本

8 篇文章 0 订阅

1. 唠叨两句

最近在看《Spring源码深度解析(第2版)》–郝佳,里面讲到的搭建spring源码环境的章节,使用的代码有点古老,配置起来很痛苦,本文使用当前(2020年9月23日)最新的5.2.x分支的源码来搭建,不需要配置乱七八糟的其他的东西,就基于IDEA2020.1就能搞定。

2. 查看源码的gradle版本

spring5.2.x源码的gradle版本
在这里插入图片描述

3. 下载源码

3.1 源码地址

github地址

3.2 通过gitee中转

3.2.1 进去github源码地址,复制URL

在这里插入图片描述

3.2.2 进入gitee,选择右上角加号,选择从GitHub/GitLab导入仓库

在这里插入图片描述

3.2.3 粘贴复制的github地址到第一栏,然后为自己的仓库命名

在这里插入图片描述
等待十几分钟,最新版的springframework就导入到gitee库中了

3.2.4 下载gitee代码到本地文件夹中

3.2.4.1 选择稳定的5.2.x版本,复制地址

在这里插入图片描述

3.2.4.2 命令行下载源码到本地文件夹
git clone -b 5.2.x https://gitee.com/scoful/spring-framework-5.2.x.git

4. 加载进IDEA

4.1 IDEA操作:File–> OPEN --> 选择下载下来的源码文件夹–>选择以项目的方式导入,选择 New Windows.

这时候会自动编译,静静等待,假如编译失败,查看这里去配置。

4.2 设置Kotlin Compiler的JVM版本为1.8

在这里插入图片描述

5. 编译项目

5.1 编译spring-oxm模块

点击右上角gradle打开编译视图,找到spring-oxm模块,然后在Tasks-other下找到compileTestjava,双击即可
在这里插入图片描述

5.2 编译spring-core模块

点击右上角gradle打开编译视图,找到spring-core模块(因为之后的spring-context依赖于core),然后在Tasks-other下找到compileTestjava,双击即可
在这里插入图片描述

5.3 编译整个工程(过程耗时间,可能10-20分钟!)

点击右上角gradle打开编译视图,找到最顶层的spring,然后在Tasks-build下找到build,双击即可
在这里插入图片描述

6. 测试源码

6.1 添加新的模块,右键最顶层目录–>New --> Module–>选择构建Gradle项目–>Java,Next,添加文件名,eg:spring-2020test

6.2 添加后,生成build.gradle和settings.gradle

6.2.1 build.gradle内容修改如下(如果没有文件自行添加)

build.gradle(相当于是pom文件),默认dependencies依赖(这里的dependencies和maven里的依赖是一样的)只有一个junit,需要手工添加spring-context,spring-beans,spring-core,spring-aop这4个核心模块

plugins {
     id 'java'
  }

  group 'org.example'
  version '1.0-SNAPSHOT'

  sourceCompatibility = 1.8

  repositories {
      mavenCentral()
  }

dependencies {
    //添加完要构建一下,否则代码中无法引用
    compile(project(":spring-context"))
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    compile(project(":spring-aop"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

6.2.2 settings.gradle内容修改如下(如果没有文件自行添加)

rootProject.name = 'spring-2020test'

6.3 打开全局配置文件:settings.gradle文件,拉到最下面,我们看到系统自动加上了spring-2020test模块

6.4 编写一个简单的代码来测试

创建一个applicationContext获取容器用的bean,用来测试Spring源码构建编译过程是否成功

6.4.1 实体类创建

package com.scoful.test01.pojo;

public class User {
	private Long id;
	private String userName;
	private int age;
	private String work;
	private double salary;

	private StringBuilder stringBuilder = new StringBuilder();

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = work;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}


	@Override
	public String toString() {

		stringBuilder.append("User :{");
		stringBuilder.append("id=" + id);
		stringBuilder.append(", userName=" + userName);
		stringBuilder.append(", age=" + age);
		stringBuilder.append(", work=" + work);
		stringBuilder.append(", salary=" + salary);
		stringBuilder.append("}");


		return stringBuilder.toString();
	}

}

6.4.2 创建配置类

package com.scoful.test01.config;

import com.scoful.test01.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.scoful.test01.**")
public class ContextConfig {

	@Bean
	public User user() {
		User user = new User();
		user.setId(1L);
		user.setUserName("刘七安");
		user.setAge(30);
		user.setSalary(20000.00);
		user.setWork("架构师");

		return user;
	}
}

6.4.3 创建启动类

package com.scoful.test01;

import com.scoful.test01.config.ContextConfig;
import com.scoful.test01.pojo.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ContextApplication {

	public static void main(String[] args) {
		System.out.println("启动");
		long startTime = System.currentTimeMillis();

		System.out.println("开始时间:" + startTime + "毫秒");

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ContextConfig.class);
		User bean = context.getBean(User.class);
		System.out.println("信息:" + bean.toString());

		long endTime = System.currentTimeMillis();

		System.out.println("结束时间:" + endTime + "毫秒");

		System.out.println("耗时:" + (endTime - startTime) + "毫秒");
	}
}

6.4.4 结果

在这里插入图片描述
end,enjoy!

如有问题参考以下链接:
链接1
链接2

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值