01--Spring源码阅读环境搭建

SpringBoot源码分析中涉及到了很多Spring的基础内容,所以中断SpringBoot的源码分析,先行分析Spring的源码

1. 本例环境

  • JDK1.8
  • IDEA 2018.2

2. 源码下载

打开https://github.com/spring-projects/spring-framework/releases下载对应源码,本例是用的是5.1.0RC1版本(该版本即SpringBoot源码分析spring-boot-2.1.0.M1使用的版本)

3.源码编译

网上有很多Spring源码的编译文章,但是建议大家首先参考Spring的官方文档
进入spring-framework-5.1.0.RC1目录,打开import-into-idea.md文件

The following has been tested against IntelliJ IDEA 2016.2.2

## Steps

_Within your locally cloned spring-framework working directory:_

1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
4. Code away

## Known issues

1. `spring-core` and `spring-oxm` should be pre-compiled due to repackaged dependencies.
See `*RepackJar` tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605).
2. `spring-aspects` does not compile due to references to aspect types unknown to
IntelliJ IDEA. See http://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the
'spring-aspects' can be excluded from the project to avoid compilation errors.
3. While JUnit tests pass from the command line with Gradle, some may fail when run from
IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within
IntelliJ IDEA, you will likely need to set the following VM options to avoid out of memory errors:
    -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m
4. If you invoke "Rebuild Project" in the IDE, you'll have to generate some test
resources of the `spring-oxm` module again (`./gradlew :spring-oxm:compileTestJava`)    


## Tips

In any case, please do not check in your own generated .iml, .ipr, or .iws files.
You'll notice these files are already intentionally in .gitignore. The same policy goes for eclipse metadata.

## FAQ

Q. What about IntelliJ IDEA's own [Gradle support](http://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)?

A. Keep an eye on http://youtrack.jetbrains.com/issue/IDEA-53476

参照文档,在spring-framework-5.1.0.RC1目录下执行./gradlew :spring-oxm:compileTestJava命令

这里写图片描述

等待一段时间,编译成功…

这里写图片描述

4.导入IDEA 注意导入的时候要选择:build.gradle

依次选择 File -> New -> Project from Existing Sources-->spring-framework-5.1.0.RC1-- build.gradle
将源码导入到IDEA
这里写图片描述
等待jar包下载完成,导入结束…

5. 运行测试

打开SimplePropertyNamespaceHandlerWithExpressionLanguageTests.java,运行测试用例即可!

6. 错误解决

  • 6.1 jdk版本问题,本例使用的是jdk1.8版本,缺少jdk9中的money包,测试用例运行编译时会有测试类报错,删除MoneyFormattingTests.java即可
  • 6.2 gradle,如果./gradlew :spring-oxm:compileTestJava命令无法下载gradle(本例用了例翻墙软件,如果不翻墙不知道能不能顺利下载下来),大家可以google gradle环境搭建即可,注意版本 4.8.1

Spring源码阅读环境搭建就介绍到这里,本例环境在搭建过程中,没有太多的错误,所以解决方案列举的较少,大家若遇到问题,可百度或google解决即可

猜你喜欢

转载自blog.csdn.net/lyc_liyanchao/article/details/82383245