Play Open
Loading Please wait Loading Please wait Loading Please wait Loading Please wait Loading Please wait Loading Please wait

spring - 源码下载与构建

一、环境准备

工欲善其事,必先利其器。

在构建spring源码前,我们首先要准备好环境。spring 5.x版本需要jdk1.8及以上版本的支持,jdk版本过低的同学请先升级。

从spring 5.0开始采用Gradle编译,所以需要先安装gradle,spring 5官方推荐的版本gradle 4.0,下载解压后按以下步骤操作即可。

第一步,配置环境变量。

第二步,添加环境变量“%GRADLE_HOME%\bin”。

第三步,检测环境,输入gradle -v命令,如果版本显示正常,说明安装成功。

二、源码下载

从Spring 3.0开始,Spring源码采用GitHub托管,不再提供官网下载链接。大家可自行去GitHub网站下载,我使用的版本为:v5.1.0.RELEASE,下载完成后,解压源码包会看到如下图所示的文件目录。

三、源码编译

第一步,修改镜像(build.gradle)。

repositories {

maven { url 'https://maven.aliyun.com/nexus/content/groups/public/'}

maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}

mavenCentral()

maven { url "https://repo.spring.io/libs-spring-framework-build" }

}

第二步,切换到项目目录,使用 gradlew :spring-oxm:compileTestJava 命令进行编译。

备注:以上报错信息无需关注

常见错误:

1.unauthorized

原因分析及解决方案:

spring.io认证失败,需要登录才能下载,用aliyun仓库替代替,修改setting.gradle。

repositories {

maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}

maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}

mavenCentral()

maven { url "https://repo.spring.io/libs-spring-framework-build" }

}

第三步,将源码导入到idea 。

按照以下步骤进行导入,导入后自动进入builder,过程较为漫长,请耐心等待。

file->new->Project from Existing Source.

构建成功

四、测试

第一步,新建模块(项目右键->new->Module)

第二步,添加依赖。

compile(project(":spring-context"))

compile(project(":spring-instrument"))

第三步,编写测试类。

public class User {

private Integer id;

private String name = "不才";

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

@Configuration

@ComponentScan("com.test.bean")

public class Test {

public static void main(String[] args) {

ApplicationContext context = new AnnotationConfigApplicationContext(Test.class);

User user = context.getBean(User.class);

System.out.println(user.getName());

}

}

正常输出,大功告成。

Posted in 23世界杯
Previous
All posts
Next