一、构建所需目录
- Gradle-7.3.3(必须,减少源码构建时的报错)
- JDK17(必须,不能用1.8)
- Spring 5.2.x(是Spring,不是Spring Boot,不过构建过程是差不多的)
- git(可选)
二、配置Gradle环境
这里不多说,网上的教程很多。不过值得注意的一点是,为了尽可能的降低之程序运行出错问题,尽量选择与你所要创建的Spring版本一样的Gradle。
我这里所构建的源码时Spring 5.2.x,官方给定的版本是Gradle-7.3.3。大家直接到Gradle逛网上下载配置即可。
三、下载Spring源码
Spring源码地址:https://github.com/spring-projects/spring-framework
等待下载完成即可
四、构建Spring源码
将下载好的Spring文件,用Idea打开。在页面初始化完成(也就是进行依赖下载时,中断下载,然后配置一下环境)
点击红色按钮,停止下载
按下图所示,配置自己的Gradle
配置项目的JDK版本及编译环境
按照下面所示进行修改
api(project(":spring-instrument"))
最后补充一下,至此便可进行开始进行构建了。按照上述步骤,不会出什么问题。由于没有配置镜像,编译时间大概需要30min,这也取决于网速。
五、在已经Build好的Spring中创建自己的测试Module
此时便创建好了,在my-spring的build.gradle,按照如下进行修改
plugins {
id 'java-library'
}
group 'org.springframework'
version '6.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
api(project(":spring-context"))
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
public class Hello {
public void sayHello() {
System.out.println("Hello, World");
}
}
public class MyApplication {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Hello hello = (Hello)ac.getBean("hello");
hello.sayHello();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="org.pxm.entity.Hello"></bean>
</beans>
六、在MyApplication点击运行
源码环境就此搭建成功!