前沿:Spring系列生态十分丰富,涉及到各个方面。但是作为Spring生态的核心基础Spring,是最重要的环节,需要理解Spring的设计原理,我们需要解读源码。
在构建Spring源码阅读环境时,遇到一些问题。通过多次尝试,慢慢找到了正确的构建编译方式,下面就记录下 ==Spring源码阅读环境的构建编译完整过程== 。【在网上也找到过许多类似的文章,但发现都不是很完整,而且讲述得也不是很清晰】
1.搭建构建前环境
-
JDK
安装JDK就不用介绍了,保证是jdk1.8以上的版本就行了。
在这里插入图片描述
-
Gradle
直接在官网下载<a href="https://gradle.org/releases/" target="_blank">gradle></a>构建工具,解压到本地后配置环境变量。增加系统变量==GRADLE_HOME=gradle的路径==,增加path= ==%GRADLE_HOME%\bin==
在这里插入图片描述
-
idea工具
使用IntelliJ IDEA 2018.3.2开发工具,<a href="https://blog.csdn.net/ganquanzhong/article/details/100146385" target="_blank">下载安装激活教程</a>。<a href="https://blog.csdn.net/ganquanzhong/article/details/100146385">idea安装教程
2.下载源码
直接去github下载源码,选择<a href="https://github.com/spring-projects/spring-framework" target="_blank">5.x</a>版本
https://github.com/spring-projects/spring-framework
https://repo.spring.io/libs-release-local/org/springframework/spring/
将下载的源码解压到本地就行,下一步就是开始构建源码了。
![](https://img.haomeiwen.com/i8928647/68095b95e93a064a.png)
3.开始构建
在cmd中进入源码目录,输入==gradlew.bat==命令
使用工具编译时,可能会出现内存溢出情况,这里我们编译时需要增加相关参数
xml -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m
使用国内的地址能明显提高依赖的下载速度
maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
发现构建gradle不通过
解决方法:关于这个问题我在网上找了很多资料,最后发现多更新几次就没问题了,主要是需要下载很多依赖包,但可能由于网速的问题导致失败。总之fail了你就多refresh几次,这个过程需要一定时间的。
等待一定时间后,构建成功!【并没有网上说的那么难,以前的版本可能需要配置很多文件。现在的版本构建很简单,需要失败可能是由下载依赖包失败导致的】
在IDEA中,如果能找到ApplicationContext类,按下Ctrl+Shift+Alt+U键,出现下图所示类图界面说明==构建成功==了!
此时可以查看Spring的源码了,但是我们需要在源码的基础上面进行修改,开发,最好将源码进行编译打包,下面就是将源码编译的过程。
4.编译源码
在构建完成源码之后,就==搭建好了阅读源码的环境==了。但是我们还可以将源码编译打包。在编译之前需要进行一些配置修改,可以查看==import-into-idea.md==文档
在编译之前还需要对==dosc.gradle==文档进行修改,因为有些注释,文件路径在编译时需要调整。
下面将两个地方的路劲替换使用windows环境下的反斜扛 ==\==
- 下面给出==task schemaZip==中修改的部分
- it.path.endsWith("META-INF\spring.schemas")
-
==it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\'))==
在这里插入图片描述
也可以用下面的方法判断是在什么环境中编译,直接粘贴复制。
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://springframework.org/schema."
duplicatesStrategy 'exclude'
//当前系统是否是windows的标志
def isWindows = System.properties['os.name'].toUpperCase().contains('WINDOWS')
//不同的操作系统,表示子目录的符号是不同的
def schemaPath = isWindows ? "META-INF\\spring.schemas" : "META-INF/spring.schemas"
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith(schemaPath)
}?.withInputStream { schemas.load(it) }
for (def key : schemas.keySet()) {
def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
assert shortName != key
File xsdFile = subproject.sourceSets.main.resources.find {
//如果是windows环境,就要对路径中的分隔符做替换
isWindows ? it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\')) : it.path.endsWith(schemas.get(key))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
接下来先编译1. Precompile spring-oxm
with ./gradlew :spring-oxm:compileTestJava
在Precompile
spring-oxm
with ./gradlew :spring-oxm:compileTestJava
编译完成后,可以编译整个工程了!(这个过程非常耗时间,可能20几分钟!)进过一段时间编译build成功!(每个人电脑的性能不一样,所需时间也不一样。 )
发现spring-aspects模块有错误, 选中该模块,右键--Load/Unload Modules ,把spring-aspects下的所有项目排除出去。
到这里,整个Spring源码的编译和环境准备就完成了,接下来我们编写一个demo来调试一下,看看我们的代码是否没问题。
4.测试
完成了上面的过程后,我们可以自己编写一个模块测试该源码构建编译过程是否完成!
-
在Spring中添加自己的module模块,同样选择gradle构建。
在这里插入图片描述
-
输入ArtufactId,工程名
在这里插入图片描述
-
选择默认的存储路径即可
在这里插入图片描述
-
确认,idea会自动帮助我们构建spring-mytest模块。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
-
下面编写一个简单的applicationContext获取容器用的bean,主要是测试Spring源码构建编译过程是否成功!
在这里插入图片描述
新建User.java
package com.gqz.springtest;
/**
* @ClassName: User
* @author: ganquanzhong
* @date: 2019/9/2 11:27
*/
public class User {
private int uid;
private String username;
private String pwd;
private String tel;
private String addr;
public User(int uid, String username, String pwd, String tel, String addr) {
this.uid = uid;
this.username = username;
this.pwd = pwd;
this.tel = tel;
this.addr = addr;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", pwd='" + pwd + '\'' +
", tel='" + tel + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
新建JavaConfig.java (使用注解的方式声明bean)
package com.gqz.springtest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName: JavaConfig
* @author: ganquanzhong
* @date: 2019/9/2 11:43
*/
@Configuration
@ComponentScan
public class JavaConfig {
@Bean
public User user(){
return new User(101,"ganquanzhong","pwd","13995978321","china");
}
}
最后写一个测试类Main.java
package com.gqz.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @ClassName: Main
* @author: ganquanzhong
* @date: 2019/9/2 12:29
*/
public class Main {
public static void main(String[] args){
ApplicationContext ac =new AnnotationConfigApplicationContext(JavaConfig.class);
User user = (User) ac.getBean("user");
System.out.println(user.toString());
}
}
- 运行
consoloe输出
User{uid=101, username='ganquanzhong', pwd='pwd', tel='13995978321', addr='china'}
4.issue
这个总结下构建源码阅读环境过程中会遇到的问题和解决办法。
-
在idea中导入spring5源码构建时,spring-core模块报错,缺失cglib相关的jar包依赖。
在这里插入图片描述
-
在idea中导入spring5源码构建时,spring-oxm模块报错,在gradle中找到spring-oxm的==genCastor、genJaxb== 命令。
在这里插入图片描述
网友评论