环境准备
- idea
- jdk 1.8+
- gradle-5.6.4
- kotlin
- git
实际上kotlin可以直接在idea上下最新。gradle在spring-framework框架中有指定固定版本。不需要独立下载
获取源码
可以通过spring-framework官网找到托管代码链接跳转到GitHub,也可以直接在github上搜索找到对应的源码,通过git的命令clone到本地。
git clone git@github.com:spring-projects/spring-framework.git
cd spring-framework
并建议checkout 5.2.x
的分支来构建,在master上构建比较复杂(并没有在master上编译过)。如果非要在master分支上构建也是可以,不过坑比较多,只能一个个去解决。
git chekcout -b 5.2.x origin/5.2.x
修改仓库信息
修改其下几个操作并不是必须。因为在构建项目时,因为网络问题或者版本问题导致不成功,修改仓库信息可以更顺畅完成整个编译过程。
- 修改
gredle-wrapper.properties
对spring-framework/gradle/wrapper/gradler-wrapper.properties
文件内容变更,变更目的是在网络不稳定情况下原地址下载gradler失败。如果你本地已经有对应版本gradler可以直接替换为本地地址。
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
#替换如下地址为本地
#distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
distributionUrl=file\://C:\User\gradle-5.6.4-bin.zip#指定自己本地gradler的5.6.4版本地址
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
- 修改grdle的仓库
对spring-framework/build.gradle
设置仓库地址,添加阿里云仓库地址'http://maven.aliyun.com/nexus/content/groups/public/'
。不添加也可以拉取库的内容,但是网络以及响应时间太长。所以能省时间为啥不添加呢?
repositories {
//添加如下一行即可,添加aliyun的仓库地址
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
对spring-oxm预编译
根据官方提示要求先对spring-oxm模块先预编译处理,具体操作如见
- 进入
spring-framework
目录下 - 执行如下gradle命令
./gradlew :spring-oxm:compileTestJava
等待进度条到100%即可,执行过程后面会有测试案例会报错误可以不用管。
导入idea
导入Idea中(File
-->New
-->project from Existing Source
-->spring-framework本地文件路径
--> Select build.gradle
),导入idea中需要一段较长时间所以耐心等待。
修改idea中jdk版本和kotlin版本
打开Settings
,win版本可以通过快捷键ctrl+alt+s
;Settings
-->Build,Execution,Deployment
-->Compiler
.
-
修改jdk编译版本
-
修改Kotlin版本
打开project Structure
,win版本可以通过快捷键ctrl+alt+shift+s
;
编译项目
准备就绪,那么就可以编译项目。
定位到spring-framework目录下
-
在
Build
-->Build project
来编译项目 -
编译过程漫长,需要耐心等待。编译成功如下显示:
测试
1、新增一个module来用测试准备,后续在模块下测试并跟踪源码。新增Module(spring-framework
-->'右键'-->New
-->Module
),新的Module选择gradle项目构建。
2、新增完之后需要等待gradle的构建。构建完成如下如:
3、在新模块中build.gradle
引进spring的模块
plugins {
id 'java'
}
group 'org.springframework'
version '5.2.14.BUILD-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
//添加spring-context的模块
compile(project(":spring-context"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
4、一切准备就绪,那么整个小demo让他跑起来。
ScanConfig:指定扫描包,让spring加载bean信息
@ComponentScan("com.cooooople.juns.service")
public class ScanConfig {
}
IndexService:被spring扫描到并加载到容器中
@Component
public class IndexService {
public IndexService() {
System.out.println("hello IndexService!");
}
public void index(){
System.out.println("invoked index method!");
}
}
CooooopleApplication:启动类
public class CooooopleApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScanConfig.class);
//获取IndexService
IndexService bean = context.getBean(IndexService.class);
//执行index方法
bean.index();
}
}
运行结果
经过一系列瞎捣鼓,直等到预期结果:
spring-framework 5.2.x源码编译就完。后续接受spring源码的摧残吧!