Spring5.1源码解析(一)源码编译导入idea

一、编译前的准备


介绍下环境

jdk使用 jdk-11.0.5

idea使用2019.2.4版本

gradle使用 gradle-5.6.3

说明idea2019.3有bug,会造成源码构建失败

1、下载gradle配置环境变量

gradle下载地址  gradle官网(https://gradle.org/releases/)

Spring5.1源码解析(一)源码编译导入idea_第1张图片

在系统高级配置里配置环境变量

我的gradle解压到了

Spring5.1源码解析(一)源码编译导入idea_第2张图片

Spring5.1源码解析(一)源码编译导入idea_第3张图片

 

Spring5.1源码解析(一)源码编译导入idea_第4张图片

 

 2、下载spring源码

访问GitHub地址https://github.com/spring-projects/spring-framework

Spring5.1源码解析(一)源码编译导入idea_第5张图片

解压spring源码

打开

Spring5.1源码解析(一)源码编译导入idea_第6张图片

Spring5.1源码解析(一)源码编译导入idea_第7张图片

针对build.gradle文件第3行,150行 配置国内镜像加速,

reference 地址

仓库名称 代理源地址 使用地址
central https://repo1.maven.org/maven2/ https://maven.aliyun.com/repository/central 或 https://maven.aliyun.com/nexus/content/repositories/central
jcenter http://jcenter.bintray.com/ https://maven.aliyun.com/repository/jcenter 或 https://maven.aliyun.com/nexus/content/repositories/jcenter
public central仓和jcenter仓的聚合仓 https://maven.aliyun.com/repository/public 或https://maven.aliyun.com/nexus/content/groups/public
google https://maven.google.com/ https://maven.aliyun.com/repository/google 或 https://maven.aliyun.com/nexus/content/repositories/google
gradle-plugin https://plugins.gradle.org/m2/ https://maven.aliyun.com/repository/gradle-plugin 或 https://maven.aliyun.com/nexus/content/repositories/gradle-plugin
spring http://repo.spring.io/libs-milestone/ https://maven.aliyun.com/repository/spring 或 https://maven.aliyun.com/nexus/content/repositories/spring
spring-plugin http://repo.spring.io/plugins-release/ https://maven.aliyun.com/repository/spring-plugin 或 https://maven.aliyun.com/nexus/content/repositories/spring-plugin
grails-core https://repo.grails.org/grails/core https://maven.aliyun.com/repository/grails-core 或 https://maven.aliyun.com/nexus/content/repositories/grails-core
apache snapshots https://repository.apache.org/snapshots/ https://maven.aliyun.com/repository/apache-snapshots 或 https://maven.aliyun.com/nexus/content/repositories/apache-snapshots

 修改后

3行

	repositories {
		maven { url "https://maven.aliyun.com/repository/spring-plugin" }
		maven { url "https://maven.aliyun.com/nexus/content/repositories/spring-plugin" }
		maven { url "https://repo.spring.io/plugins-release" }
	}

150行处

	repositories {
	    maven { url "https://maven.aliyun.com/repository/central" }
		maven { url "https://repo.spring.io/libs-release" }
		mavenLocal()
	}

 二、导入源码

打开导入源码

Spring5.1源码解析(一)源码编译导入idea_第8张图片

Spring5.1源码解析(一)源码编译导入idea_第9张图片

导入之后开始编译,我第一次编译出现如下错误。

Spring5.1源码解析(一)源码编译导入idea_第10张图片

修改配置

Spring5.1源码解析(一)源码编译导入idea_第11张图片

,重新build 

编译成功

Spring5.1源码解析(一)源码编译导入idea_第12张图片

Spring5.1源码解析(一)源码编译导入idea_第13张图片

 

 编译spring-core

Spring5.1源码解析(一)源码编译导入idea_第14张图片

 编译成功

Spring5.1源码解析(一)源码编译导入idea_第15张图片

 编译spring-context

Spring5.1源码解析(一)源码编译导入idea_第16张图片

 构建成功

Spring5.1源码解析(一)源码编译导入idea_第17张图片

 三、新建子模块

Spring5.1源码解析(一)源码编译导入idea_第18张图片

 

Spring5.1源码解析(一)源码编译导入idea_第19张图片

下一步

Spring5.1源码解析(一)源码编译导入idea_第20张图片

 下一步,finish

建立成功之后会自动打开各个子模块

Spring5.1源码解析(一)源码编译导入idea_第21张图片

 

在spring-itdog字模块中引入其他依赖

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

Spring5.1源码解析(一)源码编译导入idea_第22张图片

新建扫描类

package com.hzau;

import org.springframework.context.annotation.ComponentScan;

/**
 * @ClassName MyCompentScan
 * @Description TODO
 * @Author yueyiming
 * @Date 2020/1/2 12:15
 * @Version 1.0
 * https://blog.csdn.net/hzau_itdog
 **/
@ComponentScan(basePackages = "com.hzau.**")
public class MyCompentScan {
}

新建实体类

package com.hzau;

import org.springframework.stereotype.Component;

/**
 * @ClassName MyBean
 * @Description TODO
 * @Author yueyiming
 * @Date 2020/1/2 12:14
 * @Version 1.0
 * https://blog.csdn.net/hzau_itdog
 **/
@Component
public class MyBean {
	public MyBean() {
	}
}

进行测试

package com.hzau;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @ClassName MyTest
 * @Description TODO
 * @Author yueyiming
 * @Date 2020/1/2 12:14
 * @Version 1.0
 * https://blog.csdn.net/hzau_itdog
 **/
public class MyTest {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyCompentScan.class);
		MyBean bean = context.getBean(MyBean.class);
		System.out.println(bean);
	}
}

结果如下:

Spring5.1源码解析(一)源码编译导入idea_第23张图片

 

你可能感兴趣的:(spring源码解析)