一、本地环境准备
1.idea版本为2020.3.3 64位
2.jdk 11
3.kotlin1.4
4.gradle5.6.4
5.spring源码版本为5.2.12release
二、下载源码
1.源码地址,
https://github.com/spring-projects/spring-framework/tree/v5.2.12.RELEASE
下载太慢,分享百度云连接
链接:https://pan.baidu.com/s/1Q2Ppy5kMxvT-bqRirKDgQQ
提取码:odrl
三、源码编译
1.修改下载下来的源码的根目录下的build.gradle文件
279行开始
repositories {
maven { url “https://repo.spring.io/plugins-release” }
}
修改为阿里云镜像地址。
repositories {
maven { url ‘https://maven.aliyun.com/nexus/content/groups/public/’ }
maven { url ‘https://maven.aliyun.com/nexus/content/repositories/jcenter’}
maven { url “https://repo.springsource.org/plugins-release” }
maven { url ‘https://maven.aliyun.com/repository/spring/’}
mavenLocal()
mavenCentral()
}
2 修改第三行,注释掉
//id 'io.spring.gradle-enterprise-conventions' version '0.0.2'
如果改行不注释报错如下
* Where:
Build file ‘D:\javaTools\ideaCode\springcode\spring-framework-5.2.12.RELEASE\build.gradle’ line: 3
* What went wrong:
Error resolving plugin [id: ‘io.spring.gradle-enterprise-conventions’, version: ‘0.0.2’]
2.预先编译oxm模块
2.1在项目根目录下输入cmd,例如我的项目路径为D:\javaTools\ideaCode\springcode\spring-framework-5.2.12.RELEASE
然后回车。
2.2 执行如下指令,需要配置环境变量,才能执行下面指令,这里就不展开,相信你们都会的就不啰嗦 了。
gradlew :spring-oxm:compileTestJava
如图就先完成一部分了预编译成功。
2.3使用idea打开工程,
在file–settings打开设置
设置gradle路径和编译jdk版本
设置java编译器版本
设置kotlin编译版本
settings–projectstructure里面设置jdk版本
2.4 注释掉springaspects模块
2.5 然后点同步项目
2.5点开build,点击编译
到此,idea的编译就完成了。
#四、新建测试模块测试spring模块#
1.设置gradle项目设置
2.新建测试module
3.我本地gradle没有生成资源目录结构,修改gradle.build,手动生成目录结构
task ‘create-dirs’ {
doLast {
sourceSets*.java.srcDirs*.each {
it.mkdirs()
}
sourceSets*.resources.srcDirs*.each {
it.mkdirs()
}
}
执行task
并且增加需要的依赖
dependencies {
compile(project(":spring-core"))
compile(project(":spring-context"))
compile(project(":spring-beans"))
}
4.运行Springmain.class
springConfig类
package com.springtest;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("com.springtest")
public class SpringConfig {
}
springBean类
package com.springtest.test;
import org.springframework.stereotype.Component;
@Component
public class SpringBeans {
public void sayHello(){
System.out.println("你好啊,hello");
}
}
Springmain类
package com.springtest.test;
import com.springtest.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
SpringBeans a =(SpringBeans) context.getBean("springBeans");
a.sayHello();
}
}
5.运行该类会报错如下。
5.1 java: 找不到符号
符号: 变量 CoroutinesUtils
位置: 类 org.springframework.core.ReactiveAdapterRegistry.CoroutinesRegistrar
解决办法:
2.再运行报错找不到另一个类
解决办法:
将spring-context.gradle文件的
下面这一行
optional(project(":spring-instrument"))
修改为
compile(project(":spring-instrument"))
6.最后运行的结果如图
最后分享本地调试成功工程
链接:https://pan.baidu.com/s/17Z3eFsK9-EAnUh1R2wVr-A
提取码:zwh7
总结:至此,项目就成功跑起来了,依赖了spring的模块,可以直接debug看到源码执行过程,方便学习源码
这次是直接解压zip包源码编译,下次再尝试直接克隆看看是否可以编译成功。其他遇到的问题就是一些gradle格式问题,导致编译失败,这个要特别注意gradle的语法格式要求严格,会报错。还有就是重试几次,可能网络问题会报错,总结下来就是要有一定的耐心去编译源码。