Spring作為JAVA學習者的必須學習和熟悉的框架,需要理解Spring的設計原理,核心設計模式,對於理解設計模式,面向對象的深入理解有着深遠持久的影響,特此首先要做到本地部署源碼開始,下面將介紹如何本地部署Spring源碼.
一. 准備工具
- 下載GIT
- 安裝JDK1.8
- 安裝下載gradle
- 安裝好IDEA
1. git
官網下載地址:https://git-scm.com/downloads
作用:代碼托管工具,代碼拉取使用
2. JDK1.8及以上版本
用於JAVA開發使用的環境
3. gradle下載及安裝
(1).官網下載地址:https://services.gradle.org/distributions/
(2).查看源碼framework-bom下的gradle下的wrapper的gradle-wrapper.properties文件,查看distributionUrl的版本,然后在官網上對應下載all的zip文件,以便出錯可以看其源碼問題.
(3).安裝成功后查看版本.
執行

1 gradle -v
如上圖表示安裝成功.
4. IDEA 2019 版本以上
本文采用的是2019的版本.
二. 下載源碼
1.官網地址:https://github.com/spring-projects/spring-framework
2.將官網地址貼到gitee上生成gitee倉庫,因為github是外國服務器,通過gitee中轉下載本地的速度會提高.
(1).進入官網地址,復制URL.

(2)進入gitee,選擇右上角加號,選擇從GITHUB/GITEE導入倉庫
(3)粘貼復制的github地址到第一欄,然后為自己的倉庫命名.
等待十幾分鍾,最新版的springframework就導入到gitee庫中了.
(4)下載gitee代碼到本地文件夾中.
由於最新的5.3版本有些JDK問題,所以這里選擇穩定的版本5.2版本,執行命令如下:

1 git clone -b 5.2.x https://gitee.com/mrliuNumberOne/spring-framework5.2.git
顯示成功后即安裝成功.
三. 裝入IDEA,構建項目
1.IDEA操作:File--> OPEN --> 選擇FRAMEWORK的項目
2.選擇以項目的方式導入,選擇 New Windows.
3.設置IDEA的GRADLE,File-->setting-->Build,Excution-->Build Tool--->Gradle
設置參數
4、設置 Kotlin Compiler,在Setting中搜索。
設置Target JVM Version 為1.8版本
5、修改framework-bome模塊-->gradle-->wrapper-->gradle-wrapper.properties

內容:

1 distributionUrl=file:///e:/dev_env/gradle-6.5.1-all.zip(這里選擇gradle的壓縮包的全路 2 徑地址)
6、修改全局(根目錄下的)build.gradle文件(這個就相當於是maven的pom文件),在文件頭部加上。
(1)、添加如下:

1 buildscript { 2 repositories { 3 // 新的spring庫 4 maven { url "https://repo.spring.io/plugins-release" } 5 } 6 }
(2)、找到如圖位置代碼,修改內容如下:

1 repositories { 2 //新增以下2個阿里雲鏡像 3 maven { url 'https://maven.aliyun.com/nexus/content/groups/public/'} 4 maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter' } 5 mavenCentral() 6 maven { url "https://repo.spring.io/libs-spring-framework-build" } 7 // Reactor 8 maven { url "https://repo.spring.io/milestone" } 9 //新增spring插件庫 10 maven { url "https://repo.spring.io/plugins-release" } 11 }
7、編譯全局代碼,選擇右側,圓圈符號編譯

四. 編譯項目
1、編譯spring-oxm模塊
點擊右上角gradle打開編譯視圖,找到spring-oxm模塊,然后在other下找到compileTestjava,雙擊即可
2、編譯spring-core模塊
點擊右上角gradle打開編譯視圖,找到spring-core模塊(因為之后的spring-context依賴於core),然后在other下找到compileTestjava,雙擊即可

3、編譯整個工程(過程耗時間,可能10-20分鍾!),如下圖:打開頂層spring->build->build,經過一段時間編譯,build成功。
五. 測試源碼
1、添加新的模塊,測試模塊,File-->New --> Module

2、選擇構建Gradle項目,Next,添加內容。
3、添加后如圖所示,生成build.gradle和settings.gradle
(1)build.gradle內容(如果沒有文件自行添加)

1 plugins { 2 id 'java' 3 } 4 5 group 'org.example' 6 version '1.0-SNAPSHOT' 7 8 sourceCompatibility = 1.8 9 10 repositories { 11 mavenCentral() 12 } 13 14 dependencies { 15 testCompile group: 'junit', name: 'junit', version: '4.12' 16 }
(2)settings.gradle內容(如果沒有文件自行添加)

1 rootProject.name = 'spring-2020test'
4、打開全局配置文件:settings.gradle文件,拉到最下面,我們看到系統自動加上了spring-2020mytest模塊
5、測試模塊spring-mytest,打開build.gradle文件(相當於是pom文件),默認dependencies依賴(這里的dependencies和maven里的依賴是一樣的)只有一個junit,需要手工添加spring-context,spring-beans,spring-core,spring-aop這4個核心模塊,具體如下:

1 dependencies { 2 3 //添加完要構建一下,否則代碼中無法引用 4 compile(project(":spring-context")) 5 compile(project(":spring-beans")) 6 compile(project(":spring-core")) 7 compile(project(":spring-aop")) 8 testCompile group: 'junit', name: 'junit', version: '4.12' 9 }
6、編寫一個簡單的applicationContext獲取容器用的bean,用來測試Spring源碼構建編譯過程是否成功
(1)實體類創建

1 package com.youlai.spring.ch01.pojo; 2 3 /** 4 * @author liuyangos8888 5 * <p> 6 * 用戶類 7 */ 8 public class User { 9 10 11 private String id; 12 private String userName; 13 private String age; 14 private String work; 15 private String salary; 16 17 private StringBuilder stringBuilder = new StringBuilder(); 18 19 public String getId() { 20 return id; 21 } 22 23 public void setId(String id) { 24 this.id = id; 25 } 26 27 public String getUserName() { 28 return userName; 29 } 30 31 public void setUserName(String userName) { 32 this.userName = userName; 33 } 34 35 public String getAge() { 36 return age; 37 } 38 39 public void setAge(String age) { 40 this.age = age; 41 } 42 43 public String getWork() { 44 return work; 45 } 46 47 public void setWork(String work) { 48 this.work = work; 49 } 50 51 public String getSalary() { 52 return salary; 53 } 54 55 public void setSalary(String salary) { 56 this.salary = salary; 57 } 58 59 60 @Override 61 public String toString() { 62 63 stringBuilder.append("User :{"); 64 stringBuilder.append("id='" + id); 65 stringBuilder.append("'\\''"); 66 stringBuilder.append(", userName='" + userName); 67 stringBuilder.append("'\\''"); 68 stringBuilder.append(", age='" + age); 69 stringBuilder.append("'\\''"); 70 stringBuilder.append(", work='" + work); 71 stringBuilder.append("'\\''"); 72 stringBuilder.append(", salary='" + salary); 73 stringBuilder.append("'\\''"); 74 stringBuilder.append("'}'"); 75 76 77 return stringBuilder.toString(); 78 } 79 }
(2)創建配置類

1 package com.youlai.spring.ch01.config; 2 3 4 import com.youlai.spring.ch01.pojo.User; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.ComponentScan; 7 import org.springframework.context.annotation.Configuration; 8 9 /** 10 * @author liuyangos8888 11 */ 12 @Configuration 13 @ComponentScan("com.youlai.spring.ch01.**") 14 public class ContextConfig { 15 16 17 @Bean 18 public User user() { 19 20 User user = new User(); 21 22 user.setId("1"); 23 user.setUserName("小明"); 24 user.setAge("18"); 25 user.setSalary("20000.00"); 26 user.setWork("架構師"); 27 28 return user; 29 } 30 }
(3)測試類

1 package com.youlai.spring.ch01; 2 3 import com.youlai.spring.ch01.config.ContextConfig; 4 import com.youlai.spring.ch01.pojo.User; 5 import com.youlai.spring.ch01.service.IUserService; 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 7 8 9 /** 10 * @author liuyangos8888 11 */ 12 public class ContextApplication { 13 14 15 public static void main(String[] args) { 16 17 System.out.println("啟動..........."); 18 19 long startTime = System.currentTimeMillis(); 20 21 System.out.println("開始時間:" + startTime + "毫秒"); 22 23 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( 24 ContextConfig.class 25 ); 26 27 User bean = context.getBean(User.class); 28 29 System.out.println("信息:" + bean.toString()); 30 31 long endTime = System.currentTimeMillis(); 32 33 System.out.println("結束時間:" + endTime + "毫秒"); 34 35 System.out.println("耗時:" + (endTime - startTime) + "毫秒"); 36 37 } 38 39 }
(4)ApplicationContext結構圖
(5)結果

六. 導入依賴jar包
1.File --> Project Structure -->Libraries--> "+"-->Java
2.進入目錄\spring-framework-5.2\spring-core\kotlin-coroutines\build\libs\kotlin-coroutines-5.2.0-SNAPSHOT.jar

3、在彈出的對話框中選擇spring-core.main,再重新build項目即可,另一個包spring-core-5.2.9.BUILD-SNAPSHOT ,同樣方法導入.
4、其他缺失包的方法同上。
附:spring源代碼各個模塊作用
主要模塊:
spring-core:核心模塊 依賴注入IOC和DI的最基本實現
spring-beans: Bean工廠與裝配
spring-context:上下文,即IOC容器
spring-context-support:對IOC的擴展,以及IOC子容器
spring-context-indexer:類管理組件和Classpath掃描
spring-expression:表達式語句
切面編程:
spring-aop:面向切面編程,CGLIB,JDKProxy
spring-aspects:集成AspectJ,Aop應用框架
spring-instrument:動態Class Loading模塊
數據訪問與集成:
spring-jdbc:提供JDBC主要實現模塊,用於簡化JDBC操作
spring-tx: spring-jdbc事務管理
spring-orm:主要集成Hibernate,jpa,jdo等
spring-oxm:將java對象映射成xml數據或將xml映射為java對象
spring-jms:發送和接受消息
web組件:
spring-web:提供了最基礎的web支持,主要建立在核心容器上
spring-webmvc:實現了spring mvc的web應用
spring-websocket:主要與前端頁的全雙工通訊協議
spring-webflux:一個新的非阻塞函數式Reactive Web框架
報文:
spring-messaging: 4.0加入的模塊,主要集成基礎報文傳送應用
測試:
spring-test:測試組件
集成兼容:
spring-framework-bom:解決不同模塊依賴版本不同問題