前沿:Spring系列生态十分丰富,涉及到各个方面。但是作为Spring生态的核心基础Spring,是最重要的环节,需要理解Spring的设计原理,我们需要解读源码。 在构建Spring源码阅读环境时,遇到一些问题。通过多次尝试,慢慢找到了正确的构建编译方式,下面就记录下 ==Spring源码阅读环境的构建编译完整过程== 。【在网上也找到过许多类似的文章,但发现都不是很完整,而且讲述得也不是很清晰】
1.搭建构建前环境
-
JDK 安装JDK就不用介绍了,保证是jdk1.8以上的版本就行了。

-
Gradle 直接在官网下载gradle>构建工具,解压到本地后配置环境变量。增加系统变量==GRADLE_HOME=gradle的路径==,增加path= ==%GRADLE_HOME%\bin==

-
idea工具 使用IntelliJ IDEA 2018.3.2开发工具,下载安装激活教程。idea安装教程
2.下载源码
直接去github下载源码,选择5.x版本 github.com/spring-proj… repo.spring.io/libs-releas…


3.开始构建
在cmd中进入源码目录,输入==gradlew.bat==命令

xml -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m


maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}






4.编译源码
在构建完成源码之后,就==搭建好了阅读源码的环境==了。但是我们还可以将源码编译打包。在编译之前需要进行一些配置修改,可以查看==import-into-idea.md==文档




- 下面给出==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


spring-oxm with ./gradlew :spring-oxm:compileTestJava编译完成后,可以编译整个工程了!(这个过程非常耗时间,可能20几分钟!)





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'}
