基于spring-framework-5.0.20.RELEASE 版本源码部署
基于spring-framework-5.0.20.RELEASE 版本源码部署
基于spring-framework-5.0.20.RELEASE 版本,其他版本可能部署步骤存在差异,高版本的需要JDK11 ,还会有各种兼容问题,最终选择了5.0.20 版本。
一、下载Spring源码
githb 下载地址: github Spring
gitee下载地址:spring-framework-5.0.20.RELEASE
二、下载gradle并配置环境变量
下载gradle
Spring 5.x开始全部都采用Gradle进行编译,Gradle 类似于maven,建议根据源码上gradle 中 gradle\wrapper\gradle-wrapper.properties 文件中配置的 版本来下载:
下载地址:Gradle下载
配置Gradle 环境变量
1、将下载的gradle-4.4.1-all.zip 剪切到 E:\tool\中并 解压到 E:\tool\gradle-4.4.1
2、桌面-我的电脑-属性-高级系统设置-环境变量 中 系统变量 中添加:
key:GRADLE_HOME value:E:\tool\gradle-4.4.1
3、用户变量 中 path 中添加 %GRADLE_HOME%\bin
4、配置好后,win+R cmd 打开控制台,输入 gradle -v
能出现以下页面说明配置成功。
配置Gradle国内仓库
在 E:\tool\gradle-4.4.1\init.d 中添加 init.gradle 文件,内容如下:
allprojects {
repositories {
maven { url 'file:///E:/tool/apache-maven-3.5.4/MavenRepo'}
mavenLocal()
maven { name "Alibaba" ; url "https://maven.aliyun.com/repository/public" }
maven { name "Bstek" ; url "https://nexus.bsdn.org/content/groups/public/" }
mavenCentral()
}
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/jcenter'}
maven { url 'https://maven.aliyun.com/nexus/content/groups/public'}
}
}
}
其中 maven url 配置的是类似 maven仓库的地址。
三、修改Spring源码中相关配置
修改 build.gradle 中 repositories 配置:
repositories {
// gradlePluginPortal()
// maven { url "https://repo.spring.io/plugins-release" }
// 配置本地maven仓库
mavenLocal()
// 配置阿里云maven仓库
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://maven.aliyun.com/nexus/content/repositories/spring-plugin" }
maven { url "https://maven.aliyun.com/nexus/content/repositories/jcenter/" }
// maven中央仓库
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
修改gradle\wrapper\gradle-wrapper.properties 中 distributionUrl 的配置
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=file:///E:/tool/gradle-4.4.1-all.zip
四、导入项目
导入
打开Idea File --> New --> Project from Existing Sources 选中解压后第三步修改过配置的 源码 ,在弹出的选择模式框中选择 Gradle
注:如果没有Gradle选项,去 file -> Setting -> Plugins 中下载 Gradle 插件
修改Idea配置
导入完成后,在 File | Settings | Build, Execution, Deployment | Build Tools | Gradle 中,更改Gradle 配置:
更改完成后点击保存,跟刷新Maven 一样刷新 Gradle 此时控制台会打印 构建信息
五、添加自己的模块
为了方便自己debug 源码,建议自己添加一个modle .
1、选中Spring-framework 项目根目录,右击 new -> Module
2、选中 Gradle java
创建完成后,我们需要对 spring-debug 中添加 spring 相关依赖,
更改 spring-debug\build.gradle 中的 配置,在 dependencies 中添加依赖:
plugins {
id 'java'
}
group 'org.springframework'
version '5.0.20.RELEASE'
repositories {
mavenCentral()
}
dependencies {
//spring 核心
implementation(project(":spring-context"))
implementation(project(":spring-beans"))
implementation(project(":spring-core"))
implementation(project(":spring-aop"))
// 引入 AspectJ 相关包
implementation 'org.aspectj:aspectjrt:1.9.7'
implementation 'org.aspectj:aspectjweaver:1.9.7'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
3、编写测试代码
package com.xiao.spring.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
}
package com.xiao.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.xiao.spring")
public class SpringConfig {
}
package com.xiao.spring;
import com.xiao.spring.config.SpringConfig;
import com.xiao.spring.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringTest {
@Test
public void springIocTest(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = applicationContext.getBean(UserService.class);
System.out.println(userService);
System.out.println("************************");
}
}
六、碰到的问题以及参考
部署期间碰到的问题以及解决
-
Error:(17, 7) Kotlin: Class ‘kotlin.Suppress’ was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16.
解决:这是 Kotlin版本不匹配,需要更新版本
参考:
spring源码编译过程问题的解决-kotlin编译问题
解决Kotlin 插件报错问题
Kotlin下载地址 -
Error:(30, 41) java: 找不到符号 符号: 类 DefaultNamingPolicy
解决:idea 导入spring源码 编译报错:
找不到符号: 类 DefaultNamingPolicy,Objenesis,InstantiatorStrategy,ObjectInstantiator 等
需要 gradle 执行 objenesisRepackJar 和 cglibRepackJar
参考:idea导入spring5.0源码报错 找不到符号 -
gradle下载spring包出现Received status code 401 from server
解决:在 build.gradle 远程仓库配置阿里云的仓库
maven { url "https://maven.aliyun.com/nexus/content/repositories/spring-plugin" }
参考:gradle下载spring包出现Received status code 401 from server
-
Error:(682, 17) java: 找不到符号 符号: 类 GroovyDynamicElementReader
原因:表示缺少groovy插件,安装一个groovy插件
参考:Spring源码部署 Groovy 相关类找不到问题解决 -
Error:(41, 38) java: 程序包org.springframework.aop.target不存在问题
原因:电脑打开文件夹,发现 org.springframework.aop 下有target 文件夹,Idea 中却没有,最终发现Idea 配置了文件夹过滤 包含了 target ,去除就好了。
idea - file -settings - File Types 将配置的 target 去掉
相关参考链接
IDEA安装Gradle,解决IDEA与Gradle版本不匹配问题
Spring源码环境搭建 -(源码下载运行)
基于Gradle搭建Spring 5.3.13-release源码阅读环境
spring搭建源码错误笔记