前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring源码搭建_spring实战

spring源码搭建_spring实战

作者头像
全栈程序员站长
发布2022-09-22 19:25:45
6610
发布2022-09-22 19:25:45
举报

大家好,又见面了,我是你们的朋友全栈君。

一、环境准备

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

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

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

第一步,配置环境变量。

spring源码搭建_spring实战
spring源码搭建_spring实战

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

spring源码搭建_spring实战
spring源码搭建_spring实战

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

spring源码搭建_spring实战
spring源码搭建_spring实战

二、源码下载

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

spring源码搭建_spring实战
spring源码搭建_spring实战

三、源码编译

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

代码语言:javascript
复制
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 命令进行编译。

spring源码搭建_spring实战
spring源码搭建_spring实战

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

常见错误:

1.unauthorized

spring源码搭建_spring实战
spring源码搭建_spring实战

原因分析及解决方案:

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

代码语言:javascript
复制
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.

spring源码搭建_spring实战
spring源码搭建_spring实战
spring源码搭建_spring实战
spring源码搭建_spring实战
spring源码搭建_spring实战
spring源码搭建_spring实战

构建成功

spring源码搭建_spring实战
spring源码搭建_spring实战

四、测试

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

spring源码搭建_spring实战
spring源码搭建_spring实战
spring源码搭建_spring实战
spring源码搭建_spring实战

第二步,添加依赖。

代码语言:javascript
复制
compile(project(":spring-context"))
compile(project(":spring-instrument"))
spring源码搭建_spring实战
spring源码搭建_spring实战

第三步,编写测试类。

代码语言:javascript
复制
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;
	}
}
代码语言:javascript
复制
@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());
	}
}

正常输出,大功告成。

spring源码搭建_spring实战
spring源码搭建_spring实战

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170030.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、环境准备
  • 二、源码下载
  • 三、源码编译
  • 四、测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档