图文

Spring 源码学习 01:源码阅读环境的搭建

Spring 源码学习 01:源码阅读环境的搭建:原封面
文章目录4 节

本文发表已超过一年,部分内容可能已过时。

Spring 源码阅读环境的搭建

前言

本文记录了 Spring 源码环境的搭建方式,以及踩过的那些坑!​当前版本:5.3.2-SNAPSHOT。

环境准备

  1. Git
  2. JDK
    1. master 分支需要 JDK 11
    2. 5.2.x 分支, JDK8 即可
  3. Gradle 6.5.1
  4. IDEA 最新 (2020.2.3)

Spring 源码仓库地址:https://github.com/spring-projects/spring-framework

下载源码

  1. clone 源码
git clone  https://github.com/spring-projects/spring-framework.git
  1. 使用 IDEA 打开

    1. Spring 源码学习配图 1
    2. Spring 源码学习配图 2
    3. 等待 IDEA 加载完成即可。

注: 也可以指定 clone 的分支

git clone -b 5.2.x  https://github.com/spring-projects/spring-framework.git

或者先 fork 到自己的仓库,然后再 clone。

Spring 源码学习配图 3

这里我是 fork 到我的仓库,然后再 clone 的。

当前 master 分支代表的版本为 5.3.2-SNAPSHOT。

执行测试

  • 在项目右键创建 module

Spring 源码学习配图 4

  • 选择 Gradle Java

Spring 源码学习配图 5

  • 创建 module

Spring 源码学习配图 6

  • 在 build.gradle 中添加配置
compile(project(":spring-context"))

Spring 源码学习配图 7

  • 创建测试类并测试

Spring 源码学习配图 8

其中 UserComponent 添加了 @Component 注解, 程序正常执行则一切 OK。可以开始愉快的调试代码了。

问题总结

编译失败

有小伙伴直接下载 zip 包,可能遇到以下问题:(非常不建议直接下载 zip 包构建,想知道原因可以继续看,最后我也没有构建成功,而是直接通过 clone 构建的。)

  1. 报错如下:
fatal: not a git repository (or any of the parent directories): .git

BUILD SUCCESSFUL in 14s
Build scan background action failed.
org.gradle.process.internal.ExecException: Process 'command 'git'' finished with non-zero exit value 128	
    ... 其他省略

Spring 源码学习配图 9

看意思是没有 git 配置,那就添加上吧!

  1. 这时候想着添加 git

VCS -> Enable Version Control Integration... -> 右上角 Reload All Gradle Projects

依然报错

fatal: Needed a single revision

Spring 源码学习配图 10

  1. 查询问题

issues 地址:https://github.com/spring-projects/spring-framework/issues/24467

建议使用

$ git clone git@github.com:spring-projects/spring-framework.git

Spring 源码学习配图 11

意思就是 zip 发行版主要是用来共享源代码,但不一定用于构建它。

  1. 最后我选择了使用 clone 的方式,直接 clone 下来,然后 build 通过。

缺少 cglib 和 objenesis 包

Kotlin: warnings found and -Weeror specified

Spring 源码学习配图 12

没有 spring-cglib-repack 和 spring-objenesis-repack 包

Spring 源码学习配图 13

执行这两个即可。

找不到包 jdk.jfr

import jdk.jfr.Category;
import jdk.jfr.Description;
import jdk.jfr.Event;
import jdk.jfr.Label;

JDK 升级为 11。因为我本地使用的是 JDK8,发现报错,jfr 包需要升级 JDK 11 才有。

如果不生效,可以通过:

IDEA -> File -> Project Structure -> Project 检查下是否修改为 JDK 11

快捷键:⌘ + ;

Spring 源码学习配图 14

相关资料

  1. Spring 仓库:https://github.com/spring-projects/spring-framework
  2. Spring 构建文档:https://github.com/spring-projects/spring-framework/wiki/Build-from-Source

历史文章

打开原图

订阅更新,继续阅读新的实践记录

最近发布

评论

隐私提示

昵称、网址和评论内容会公开显示,邮箱不公开。请勿在评论中填写敏感信息。