转:https://blog.csdn.net/z_c8819/article/details/105258015

 

1.从GitHub上下载spring项目

https://github.com/spring-projects/spring-framework


当然也可以使用git进行下载:

 

git clone --branch v5.2.2.RELEASE https://github.com/spring-projects/spring-framework.git

 

 我下载的5.2.2


2.安装Gradle

2.1下载Gradle
注意:由于spring源码是基于Gradle所以需要安装配置gradle 注意:jdk版本要1.8及以上版本
gradle下载地址:https://services.gradle.org/distributions/,选择合适的gradle版本,本人用的是6.0的版本


2.2 gradle解压并配置环境变量

直接解压,然后配置环境变量

 

 

 

 

 

 



2.3 配置 GRADLE_USER_HOME

 

 


找到一个盘符空间比较大的地方来重新放置.gradle文件
当然也可不用配置,那么下载的.gradle文件会存放在C盘

 

2.4 验证

cmd 命令下运行:gradle -v

 

 

 

3 编译spring源码
3.1 进入spring源码目录,载目录下有一个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 https://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](https://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)?

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

里面有一条命令:gradlew :spring-oxm:compileTestJava

3.2 在cmd中进入源码所在目录执行此命令下载jar

  此过程会耗时很久,因个人网速情况而定

 

 

 

 

 

 


3.3 使用idea打开spring源码,需要在idea中安装插件kotlin

 

 

 

4.替换jar包

  我们需要线在springframework项目里面生成jar包,放入到我们需要的项目中去,这样子我们的项目就可以在源码里面写注释了。

  以spring-context为例

 

4.1 生成jar包

4.1.1 打开gradle窗口

 

 

 

4.1.2 在gradle窗口下找到spring-context的jar包

 

 

 

4.1.3 双击这个jar包

然后在项目里面可以看到生成了一个jar包

 

 

 

 

4.2 在自己的项目导入刚才生成的jar包

4.2.1 打开自己的项目(注意,使用的spring版本需要和上面的一致)

 

 

 

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
    </dependencies>

 

4.2.2 打开External Libraries,找到spring-context的jar包

 

 

 

4.2.3 右键+Open Library Settings

 

 

 

4.2.4 替换class下面的jar包

选择刚才在springframe项目里面生成的jar包

 

 

 再删除原有的

 

4.2.5 替换resource下面的

这里只需要选择到spring-context目录即可

 

 

 删除原来的


5 此时context下面的类全都可以写注释了

 

 

 

 

 

 

其它的jar包同上