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/

在系統高級配置裏配置環境變量

我的gradle解壓到了

 

 

 2、下載spring源碼

訪問GitHub地址https://github.com/spring-projects/spring-framework

解壓spring源碼

打開

針對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()
	}

 二、導入源碼

打開導入源碼

導入之後開始編譯,我第一次編譯出現如下錯誤。

修改配置

,重新build 

編譯成功

 

 編譯spring-core

 編譯成功

 編譯spring-context

 構建成功

 三、新建子模塊

 

下一步

 下一步,finish

建立成功之後會自動打開各個子模塊

 

在spring-itdog字模塊中引入其他依賴

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

新建掃描類

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);
	}
}

結果如下:

 

發佈了61 篇原創文章 · 獲贊 48 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章