0 | Spring:源代码下载、导入与Hello World

最近确实受到一些打击,开始思考人生、也开始思考自己到底缺的是什么。决定从spring源代码开始学习,是我在思考后,作出的决定。但是同时我也不会放弃netty,等稍微有点时间了,我会继续开始netty相关的学习。说起来很是惭愧,因为项目中每天都在用这个框架,也觉得自己大致懂spring是干什么的,除了

最近确实受到一些打击,开始思考人生、也开始思考自己到底缺的是什么。决定从spring源代码开始学习,是我在思考后,作出的决定。但是同时我也不会放弃netty,等稍微有点时间了,我会继续开始netty相关的学习。

说起来很是惭愧,因为项目中每天都在用这个框架,也觉得自己大致懂spring是干什么的,除了偶尔看看SpringMVC的代码外,很少看过spring以及SpringBoot相关的源码,以致于连SpringBoot的自动配置也只是知道个大概。最近几天看了看SpringBoot的自动配置源码,算是从代码上有了一个了解,但是同时感觉自己对每天都在用到的Spring还所知盛少。所以,在这样的基础上,还去看netty,有一种眼高手低、“起房子我只要顶楼”的感觉。所以还是从头开始吧,毕竟这个框架,在毕业前就一致被问来问去,到现在都还没说太清楚,确实很是惭愧。

源代码的克隆

https://github.com/spring-projects/spring-framework 克隆源代码到本地,然后导入到idea中。

如果从github上面克隆比较慢,可以先在github上面fork一份源代码到自己的账号上;

然后在gitee上,导入绑定的github账号下的项目,这个速度比较快;

最后直接从gitee上面克隆。

git clone [email protected]:sasurai/spring-framework.git

这样操作的话,速度比较快。

源代码的导入

看多了网上的各种教程,包括书上面的,都没有官方导入说明来得直接。

The following has been tested against IntelliJ IDEA 2016.2.2

Steps

Within your locally cloned spring-framework working directory:
  1. Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
  2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
  3. When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
  4. Code away

Known issues

  1. spring-core and spring-oxm should be pre-compiled due to repackaged dependencies. See *RepackJar tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605).
  2. spring-aspects does not compile due to references to aspect types unknown to IntelliJ IDEA. See https://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the 'spring-aspects' can be excluded from the project to avoid compilation errors.
  3. While JUnit tests pass from the command line with Gradle, some may fail when run from IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within IntelliJ IDEA, you will likely need to set the following VM options to avoid out of memory errors: -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m
  4. If you invoke "Rebuild Project" in the IDE, you'll have to generate some test resources of the spring-oxm module again (./gradlew :spring-oxm:compileTestJava)

Tips

In any case, please do not check in your own generated .iml, .ipr, or .iws files. You'll notice these files are already intentionally in .gitignore. The same policy goes for eclipse metadata.

FAQ

Q. What about IntelliJ IDEA's own Gradle support?
A. Keep an eye on https://youtrack.jetbrains.com/issue/IDEA-53476

与maven类似,import之后会使用gradle构建整个项目,这个过程中会下载依赖,默认的依赖下载网址是国外的,很慢,所以构建需要很长时间,甚至出现构建失败。

多试几次

使用阿里镜像

在用户目录下的.gradle中,新建init.gradle文件:vim ~/.gradle/init.gradle

内容是从网上抄过来的,内容如下:

allprojects {
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        all {
            ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                        remove repo
                    }
                    if (url.startsWith('https://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                        remove repo
                    }
                }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

架个梯子(尝试了,但是想过不太理想,可能是梯子本身太慢)

构建完成后,各个模块能够突出显示出来,与用maven构建后的效果类似。

来一个基于源码的Hello World

新建一个module,然后在新建module的build.gradle中,加入对spring-framework中子模块的依赖

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile project(":spring-core")
    compile project(":spring-beans")
    compile project(":spring-context")
}

只要注意这个写法就行了,与maven中加入依赖的效果是类似的,然后新建一个bean类

package main.beans;

public class TestBean {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "TestBean{" +
				"name='" + name + '\'' +
				'}';
	}
}

xml配置文件。这段是从书上敲过来的,太坑了,连大小写都还要再改,搞得一脸懵逼。

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="testBean" class="main.beans.TestBean"/>
</beans>

测试类

package main;

import main.beans.TestBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMainApplication {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext("beanFactoryTest.xml");
		TestBean testBean = beanFactory.getBean(TestBean.class);
		System.out.println(testBean);
	}
}

启动测试类,输出如下:

愉快地开始spring吧!

Read more

Volcano 与 Kubernetes GPU 调度学习笔记

本笔记系统整理 Volcano 调度器、Kubernetes 调度框架、GPU Device Plugin、HAMi 等云原生 AI 调度领域的核心知识,适合用于学习、复习和工程实践参考。 目录 * 第一部分:Volcano 入门 * 1. Volcano 是什么 * 2. 安装与快速使用 * 3. 核心特性一览 * 第二部分:Volcano 整体架构 * 4. Volcano 解决的核心问题 * 5. 整体架构与数据流 * 6. 三层抽象模型 * 第三部分:Volcano 核心实现原理 * 7. Session 机制 * 8. Gang Scheduling 实现 * 9. Queue 与 DRF 公平调度

容器镜像(4):镜像的常用工具箱

容器镜像(4):镜像的常用工具箱

前几篇在讲多架构镜像时已经用过 skopeo 和 crane 做镜像复制,这篇系统整理这两个工具的完整能力,同时介绍几个日常操作镜像时同样好用的工具。 一、skopeo:不依赖 Daemon 的镜像瑞士军刀 skopeo 的核心价值是绕过 Docker daemon,直接与 Registry API 交互。上一篇用它做镜像复制和离线传输,但它的能力远不止于此。 1.1 安装 # Ubuntu / Debian sudo apt install -y skopeo skopeo --version # skopeo version 1.15.1 1.2 inspect:免拉取检查镜像元数据 docker inspect 需要先把镜像拉到本地,skopeo inspect 直接向 Registry

容器镜像(3):多架构镜像构建

容器镜像(3):多架构镜像构建

一、什么是多架构镜像 1.1 OCI Image Index 上一篇介绍了单平台镜像的结构:一个 Manifest 指向 Config 和若干 Layer blob。多架构镜像在此之上多了一层——OCI Image Index(也叫 Manifest List),是一个轻量的索引文件,把多个单平台 Manifest 组织在一起: $ docker manifest inspect golang:1.22-alpine { "schemaVersion": 2, "mediaType": "application/vnd.oci.image.index.v1+json", "manifests&

容器镜像(2):containerd 视角下的镜像

容器镜像(2):containerd 视角下的镜像

一、为什么需要了解 containerd 如果你只用 docker run 跑容器,从来不关心底层,那可以不了解 containerd。但如果你在用 Kubernetes,或者想真正理解"容器运行时"是什么,containerd 是绕不开的。 事实上,当你执行 docker run 的时候,containerd 早就在后台悄悄工作了——Docker 从 1.11 版本开始,就把核心运行时剥离出来交给 containerd 负责。 1.1 Docker 的架构演变 早期的 Docker(1.10 及之前)是一个"大一统"的单体程序:一个 dockerd