概述
对于一门技术,我们最先是了解它(what),然后再熟练的使用它(how)以及何时用它(when),最后肯定要看透它(why)。
spring作为Java开发人员可以说是最熟悉不过的了,基本每个Java的项目都会用到spring,阅读spring的源码,对于一个有理想的程序员来说,也是一件必干不可的事,阅读源码我们才能更好的使用它、理解它,在遇到问题的时候,我们才能更好的找到问题的原因,并高效的解决。
阅读源码的最好方式,就是下载下来然后导入到IDE,通过编写Test代码来Debug跟踪。spring源码导入到eclipse确实要花费一点功夫,容易出错,在这里记录一下。
基本步骤,如下:
- 去GitHub中下载spring framework的源码,下载Spring其中的一个版本,本文下载的 Spring5.0.5 ;
- 安装Gradle软件, 解压,然后设置GRADLE_HOME和PATH;
- 命令行中运行gradle -v,检查一下是否正确安装配置;
- 命令行中运行spring framework根目录下的 import-into-eclipse.bat ,连续两次回车,就可以开始自动构建,如果过程中出现失败的情况,就要根据具体提示来解决了;中间出现停止不前的情况里,按ctrl+c终止,再运行就可以进行下去;过程需要花些时间(几个小时都有可能),视下载各种依赖jar包和dom文件的速度而定;
- 在Eclipse菜单File->import选择General下面的Exsiting projects into workspace,这样就导入完成了。
如果你按上述步骤能一次成功的话,那不仅是你运气,而且RP极高,反正我没有,而且还折腾了一下午的时间。
第一步:下载 spring 项目
下载后如下压缩包,解压即可。
第二步:下载 gradle 软件,配置环境变量。
第三步: 检查gradle软件是否安装成功。
第四步:构建项目
运行源码中的 import-into-eclipse.bat,来构建项目,
或者运行 进入spring源码目录执行:gradle eclipse -x :eclipse ,来构建项目。
-----------------------------------------------------------------------Spring Framework - Eclipse/STS project import guideThis script will guide you through the process of importing the SpringFramework projects into Eclipse or the Spring Tool Suite (STS). It isrecommended that you have a recent version of Eclipse or STS. As a bareminimum you will need Eclipse with full Java 8 support, the AspectJDevelopment Tools (AJDT), and the Groovy Compiler.If you need to download and install Eclipse or STS, please do that nowby visiting one of the following sites:- Eclipse downloads: http://download.eclipse.org/eclipse/downloads- STS downloads: http://spring.io/tools/sts/all- STS nightly builds: http://dist.springsource.com/snapshot/STS/nightly-distributions.html- ADJT: http://www.eclipse.org/ajdt/downloads/- Groovy Eclipse: https://github.com/groovy/groovy-eclipse/wikiOtherwise, press enter and we'll begin.请按任意键继续. . .-----------------------------------------------------------------------STEP 1: Generate subproject Eclipse metadataThe first step will be to generate Eclipse project metadata for eachof the spring-* subprojects. This happens via the built-in"Gradle wrapper" script (./gradlew in this directory). If this is yourfirst time using the Gradle wrapper, this step may take a few minuteswhile a Gradle distribution is downloaded for you.The command run will be: gradlew --no-daemon cleanEclipse :spring-oxm:compileTestJava eclipse -x :eclipsePress enter when ready.请按任意键继续. . .:buildSrc:clean:buildSrc:compileJava NO-SOURCE:buildSrc:compileGroovy:buildSrc:processResources:buildSrc:classes:buildSrc:jar:buildSrc:assemble:buildSrc:compileTestJava NO-SOURCE:buildSrc:compileTestGroovy NO-SOURCE:buildSrc:processTestResources NO-SOURCE:buildSrc:testClasses UP-TO-DATE:buildSrc:test NO-SOURCE:buildSrc:check UP-TO-DATE:buildSrc:buildDownload https://plugins.gradle.org/m2/org/jetbrains/dokka/org.jetbrains.dokka.gradle.plugin/0.9.15/org.jetbrains.dokka.gradle.plugin-0.9.15.pomDownload https://jcenter.bintray.com/org/jetbrains/dokka/dokka-gradle-plugin/0.9.15/dokka-gradle-plugin-0.9.15.pomDownload https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-runtime/1.0.7/kotlin-runtime-1.0.7.pomDownload https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-project/1.0.7/kotlin-project-1.0.7.pomDownload https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-reflect/1.0.7/kotlin-reflect-1.0.7.pomDownload https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-runtime/1.0.7/kotlin-runtime-1.0.7.jarDownload https://jcenter.bintray.com/org/jetbrains/dokka/dokka-gradle-plugin/0.9.15/dokka-gradle-plugin-0.9.15.jar:cleanEclipseClasspath UP-TO-DATE:cleanEclipseJdt UP-TO-DATE:cleanEclipseJdtUi UP-TO-DATE:cleanEclipseProject UP-TO-DATE:cleanEclipse UP-TO-DATE:spring-aop:cleanEclipseClasspath UP-TO-DATE:spring-aop:cleanEclipseJdt UP-TO-DATE:spring-aop:cleanEclipseJdtUi UP-TO-DATE:spring-aop:cleanEclipseProject UP-TO-DATE:spring-aop:cleanEclipse UP-TO-DATE:spring-aspects:cleanEclipseClasspath UP-TO-DATE:spring-aspects:cleanEclipseJdt UP-TO-DATE:spring-aspects:cleanEclipseJdtUi UP-TO-DATE:spring-aspects:cleanEclipseProject UP-TO-DATE:spring-aspects:cleanEclipse UP-TO-DATE:spring-beans:cleanEclipseClasspath UP-TO-DATE:spring-beans:cleanEclipseJdt UP-TO-DATE:spring-beans:cleanEclipseJdtUi UP-TO-DATE:spring-beans:cleanEclipseProject UP-TO-DATE:spring-beans:cleanEclipse UP-TO-DATE:spring-build-src:cleanEclipseClasspath UP-TO-DATE:spring-build-src:cleanEclipseJdt UP-TO-DATE:spring-build-src:cleanEclipseJdtUi UP-TO-DATE:spring-build-src:cleanEclipseProject UP-TO-DATE:spring-build-src:cleanEclipse UP-TO-DATE:spring-context:cleanEclipseClasspath UP-TO-DATE:spring-context:cleanEclipseJdt UP-TO-DATE:spring-context:cleanEclipseJdtUi UP-TO-DATE:spring-context:cleanEclipseProject UP-TO-DATE:spring-context:cleanEclipse UP-TO-DATE:spring-context-indexer:cleanEclipseClasspath UP-TO-DATE:spring-context-indexer:cleanEclipseJdt UP-TO-DATE:spring-context-indexer:cleanEclipseJdtUi UP-TO-DATE:spring-context-indexer:cleanEclipseProject UP-TO-DATE:spring-context-indexer:cleanEclipse UP-TO-DATE:spring-context-support:cleanEclipseClasspath UP-TO-DATE:spring-context-support:cleanEclipseJdt UP-TO-DATE:spring-context-support:cleanEclipseJdtUi UP-TO-DATE:spring-context-support:cleanEclipseProject UP-TO-DATE:spring-context-support:cleanEclipse UP-TO-DATE:spring-core:cleanEclipseClasspath UP-TO-DATE:spring-core:cleanEclipseJdt UP-TO-DATE:spring-core:cleanEclipseJdtUi UP-TO-DATE:spring-core:cleanEclipseProject UP-TO-DATE:spring-core:cleanEclipse UP-TO-DATE:spring-expression:cleanEclipseClasspath UP-TO-DATE:spring-expression:cleanEclipseJdt UP-TO-DATE:spring-expression:cleanEclipseJdtUi UP-TO-DATE:spring-expression:cleanEclipseProject UP-TO-DATE:spring-expression:cleanEclipse UP-TO-DATE:spring-framework-bom:cleanEclipseClasspath UP-TO-DATE:spring-framework-bom:cleanEclipseJdt UP-TO-DATE:spring-framework-bom:cleanEclipseJdtUi UP-TO-DATE:spring-framework-bom:cleanEclipseProject UP-TO-DATE:spring-framework-bom:cleanEclipse UP-TO-DATE:spring-instrument:cleanEclipseClasspath UP-TO-DATE:spring-instrument:cleanEclipseJdt UP-TO-DATE:spring-instrument:cleanEclipseJdtUi UP-TO-DATE:spring-instrument:cleanEclipseProject UP-TO-DATE:spring-instrument:cleanEclipse UP-TO-DATE:spring-jcl:cleanEclipseClasspath UP-TO-DATE:spring-jcl:cleanEclipseJdt UP-TO-DATE:spring-jcl:cleanEclipseJdtUi UP-TO-DATE:spring-jcl:cleanEclipseProject UP-TO-DATE:spring-jcl:cleanEclipse UP-TO-DATE:spring-jdbc:cleanEclipseClasspath UP-TO-DATE:spring-jdbc:cleanEclipseJdt UP-TO-DATE:spring-jdbc:cleanEclipseJdtUi UP-TO-DATE:spring-jdbc:cleanEclipseProject UP-TO-DATE:spring-jdbc:cleanEclipse UP-TO-DATE:spring-jms:cleanEclipseClasspath UP-TO-DATE:spring-jms:cleanEclipseJdt UP-TO-DATE:spring-jms:cleanEclipseJdtUi UP-TO-DATE:spring-jms:cleanEclipseProject UP-TO-DATE:spring-jms:cleanEclipse UP-TO-DATE:spring-messaging:cleanEclipseClasspath UP-TO-DATE:spring-messaging:cleanEclipseJdt UP-TO-DATE:spring-messaging:cleanEclipseJdtUi UP-TO-DATE:spring-messaging:cleanEclipseProject UP-TO-DATE:spring-messaging:cleanEclipse UP-TO-DATE:spring-orm:cleanEclipseClasspath UP-TO-DATE:spring-orm:cleanEclipseJdt UP-TO-DATE:spring-orm:cleanEclipseJdtUi UP-TO-DATE:spring-orm:cleanEclipseProject UP-TO-DATE:spring-orm:cleanEclipse UP-TO-DATE:spring-oxm:cleanEclipseClasspath UP-TO-DATE:spring-oxm:cleanEclipseJdt UP-TO-DATE:spring-oxm:cleanEclipseJdtUi UP-TO-DATE:spring-oxm:cleanEclipseProject UP-TO-DATE:spring-oxm:cleanEclipse UP-TO-DATE:spring-test:cleanEclipseClasspath UP-TO-DATE:spring-test:cleanEclipseJdt UP-TO-DATE:spring-test:cleanEclipseJdtUi UP-TO-DATE:spring-test:cleanEclipseProject UP-TO-DATE:spring-test:cleanEclipse UP-TO-DATE:spring-tx:cleanEclipseClasspath UP-TO-DATE:spring-tx:cleanEclipseJdt UP-TO-DATE:spring-tx:cleanEclipseJdtUi UP-TO-DATE:spring-tx:cleanEclipseProject UP-TO-DATE:spring-tx:cleanEclipse UP-TO-DATE:spring-web:cleanEclipseClasspath UP-TO-DATE:spring-web:cleanEclipseJdt UP-TO-DATE:spring-web:cleanEclipseJdtUi UP-TO-DATE:spring-web:cleanEclipseProject UP-TO-DATE:spring-web:cleanEclipse UP-TO-DATE:spring-webflux:cleanEclipseClasspath UP-TO-DATE:spring-webflux:cleanEclipseJdt UP-TO-DATE:spring-webflux:cleanEclipseJdtUi UP-TO-DATE:spring-webflux:cleanEclipseProject UP-TO-DATE:spring-webflux:cleanEclipse UP-TO-DATE:spring-webmvc:cleanEclipseClasspath UP-TO-DATE:spring-webmvc:cleanEclipseJdt UP-TO-DATE:spring-webmvc:cleanEclipseJdtUi UP-TO-DATE:spring-webmvc:cleanEclipseProject UP-TO-DATE:spring-webmvc:cleanEclipse UP-TO-DATE:spring-websocket:cleanEclipseClasspath UP-TO-DATE:spring-websocket:cleanEclipseJdt UP-TO-DATE:spring-websocket:cleanEclipseJdtUi UP-TO-DATE:spring-websocket:cleanEclipseProject UP-TO-DATE:spring-websocket:cleanEclipse UP-TO-DATE:spring-core:cglibRepackJarDownload https://repo.spring.io/libs-release/com/googlecode/jarjar/jarjar/1.3/jarjar-1.3.pomDownload https://repo.spring.io/libs-release/org/sonatype/oss/oss-parent/7/oss-parent-7.pomDownload https://repo.spring.io/libs-release/com/googlecode/jarjar/jarjar/1.3/jarjar-1.3.jarDownload https://repo.spring.io/libs-release/cglib/cglib/3.2.6/cglib-3.2.6.pomDownload https://repo.spring.io/libs-release/cglib/cglib-parent/3.2.6/cglib-parent-3.2.6.pomDownload https://repo.spring.io/libs-release/cglib/cglib/3.2.6/cglib-3.2.6.jar:spring-core:objenesisRepackJar:spring-jcl:compileKotlinDownload https://repo.spring.io/libs-release/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.pomDownload https://repo.spring.io/libs-release/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jarDownload https://repo.spring.io/libs-release/org/slf4j/slf4j-parent/1.7.25/slf4j-parent-1.7.25.pom:spring-jcl:compileKotlin FAILEDFAILURE: Build failed with an exception.* What went wrong:Execution failed for task ':spring-jcl:compileKotlin'.> Could not resolve all files for configuration ':spring-jcl:optional'. > Could not resolve org.apache.logging.log4j:log4j-api:2.10.0. Required by: project :spring-jcl > Could not resolve org.apache.logging.log4j:log4j-api:2.10.0. > Could not get resource 'https://repo.spring.io/libs-release/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.pom'. > Could not GET 'https://repo.spring.io/libs-release/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.pom'. > Connect to repo.spring.io:443 [repo.spring.io/35.186.232.213] failed: Connection timed out: connect* Try:Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with--scan to get full insights.* Get more help at https://help.gradle.orgBUILD FAILED in 1m 39s99 actionable tasks: 3 executed, 96 up-to-dateD:\f\src\spring-framework-master>
上面的提示,告诉我们依赖包下载失败了,这个会影响导入工程后spring项目中缺少jar包的支持,根据提示解决即可。
第五步:导入eclipse
若eclipse没有安装gradle插件,那么安装一下即可,
操作如下:
打开eclipse,Help——Marketplace,搜索buildship,进行安装。安装成功后会重启eclipse。
点完成后等待如下界面,