JDK:1.8.0_241
下载地址:https://www.oracle.com/java/technologies/javase-downloads.html
注意:JDK1.8.0_25建议不要使用,本人亲测编译spring-context时报错
IntelliJ IDEA版本:IntelliJ IDEA 2019.3.3 (Ultimate Edition)
GRADLE版本:4.9
Spring版本:5.1.12.BUILD-SNAPSHOT
1. spring源码下载及说明
2.gradle配置
环境变量path添加:%GRADLE_HOME%\bin;
打开cmd窗口,输入:gradle -v , 出现如下图,则表示配置成功
- Build成功
build成功后,左侧Spring子模块文件夹右下角会有蓝色小方块,右侧点击Gradle会所有已构建出来Spring子模块
2.docs.gradle文件修改
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'
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith("META-INF\\spring.schemas")
}?.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 {
it.path.endsWith(schemas.get(key).replaceAll('\\/', '\\\\'))
}
assert xsdFile != null
into(shortName) {
from xsdFile.path
}
}
}
}
1.spring-oxm、spring-ore模块预编译
根据import-into-idea.md文档里面介绍,需要先预编译spring-oxm和spring-core,因为重新打包的时候需要依赖他们。
预编译有idea这么强大的工具,就不需要输入命令编译了,预编译如下:
编译成功!
Spring-oxm预编译成功后,按照同样的方法预编译Spring-core.
2.终极构建
最后一步,此过程也比较慢,如果有异常会抛出到控制台。
构建成功后可以进行单元测试
3.单元测试
使用DispatcherServletTests进行单元测试,测试执行,证明Spring源码阅读环境搭建成功
测试成功!
compile(project(":spring-context"))
compile(project(":spring-beans"))
compile(project(":spring-core"))
compile(project(":spring-aop"))
package com.cjk.springtest;
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类
package com.cjk.springtest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class JavaConfig {
@Bean
public User user(){
return new User(1,"chenjinkun","123456","186****8882","china.yunnan.kunming");
}
}
创建测试入口类
package com.cjk.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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());
}
}
在其他地方看到别人说构建报错或者是运行报错,把spring-aspects或者是其他报错的模块删除。不需要删除任何模块,按照上述方法就可以构建成功,上述方法未删除过任何模块,只添加过一个测试模块:spring-mytest。上述方法我在公司和家里都测试过(除IDEA外其他工具、源码版本都相同)。
我觉得构建和编译能否成功的主要因素还是网络。
至此源码构建、编译、测试成功!各位请准备好被源码吊起来捶打吧,当然呢也会有不菲的收获。
祝好运!