调试Spring源码 5.2.2.RELEASE

本文介绍了如何配置和搭建Spring框架的源码调试环境,包括下载所需版本的Spring和Gradle,修改配置文件以适应国内镜像,并解决编译过程中的错误。在IDEA中新建测试模块,添加依赖后,成功启动并调试Spring源码。
摘要由CSDN通过智能技术生成

1. 环境准备

2. Spring 环境配置

  • 在 gradle 目录 init.d 底下新建文件 init.gradle,把下面的内容复制进去
   allprojects{
    repositories {
        def REPOSITORY_URL = 'https://maven.aliyun.com/nexus/content/groups/public/'
        all { ArtifactRepository repo ->
            def url = repo.url.toString()
            if ((repo instanceof MavenArtifactRepository) && (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com'))) {
                project.logger.lifecycle 'Repository ${repo.url} replaced by $REPOSITORY_URL .'
                remove repo
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}
  • 解压上一步下载的 spring 源码压缩包,进入根目录
  • 找到 settings.gradle,修改 repositories 如下
pluginManagement {
	repositories {
		gradlePluginPortal()
		maven { url 'https://maven.aliyun.com/repository/public' }
		maven { url 'https://repo.spring.io/plugins-release' }
	}
}
  • 找到 gradle.properties,修改内容如下:
version=5.2.2.RELEASE
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.caching=true
org.gradle.parallel=true
  • 找到 build.gradle,修改 buildscript.repositories 内容如下。底下还有一个 repositories ,也改成一样的。
buildscript {
	repositories {
		maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
		maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
		mavenCentral()
		jcenter()
		google()
		maven { url "https://repo.spring.io/libs-spring-framework-build" }
	}

	dependencies {
		classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
		classpath 'io.spring.asciidoctor:spring-asciidoctor-extensions:0.1.3.RELEASE'
	}
}
  • 进入 spring-framework\gradle\wrapper 目录,将前面下载的 gradle-5.6.4-bin.zip 复制过来。并修改文件 gradle-wrapper.properties,内容如下:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
  • 回到 spring 根目录下,执行如下命令
gradle :spring-oxm:compileTestJava
  • 等待编译完毕即可,成功之后会出现 BUILD SUCCESSFUL

注意:spring和gradle的版本是有对应关系的,如果报错提示build_scan_plugin版本问题,一般是gradle版本跟spring版本不符。根据报错信息,参考这里重新下载:https://docs.gradle.com/enterprise/compatibility/#build_scan_plugin

3. 调试源码

IDEA打开spring-framework,新建一个我们自己的测试模块。File -> New Module 选择Gradle项目,勾选Web。
在这里插入图片描述
修改 build.gradle,添加 spring-context 依赖:
在这里插入图片描述

随后,写一个简单的main程序启动试试:

在这里插入图片描述

我这边一开始启动是报错的,有以下2个问题:

报错1:

Error:(354, 51) java: 找不到符号
符号: 变量 CoroutinesUtils
位置: 类 org.springframework.core.ReactiveAdapterRegistry.CoroutinesRegistrar

解决方法:

File -> Project Structure -> Libraries -> + > Java,然后选择spring-framework/spring-core/kotlin-coroutines/build/libs/kotlin-coroutines-5.2.2.RELEASE.jar,在弹出的对话框中选择 spring.spring-core.main

然后重新编译。

报错2:

Error:(26, 38) java: 找不到符号
符号: 类 InstrumentationSavingAgent
位置: 程序包 org.springframework.instrument

解决方法:

在刚刚新建的模块下,添加依赖 spring-instrument ,如下:

在这里插入图片描述
重新执行main方法,启动成功啦

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

°Fuhb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值