Spring原始碼閱讀 ------------------- SpringFrameWork 5.2 +IDEA 部署
Spring作為JAVA學習者的必須學習和熟悉的框架,需要理解Spring的設計原理,核心設計模式,對於理解設計模式,面向物件的深入理解有著深遠持久的影響,特此首先要做到本地部署原始碼開始,下面將介紹如何本地部署Spring原始碼.
一. 準備工具
- 下載GIT
- 安裝JDK1.8
- 安裝下載gradle
- 安裝好IDEA
1. git
官網下載地址:https://git-scm.com/downloads
作用:程式碼託管工具,程式碼拉取使用
2. JDK1.8及以上版本
用於JAVA開發使用的環境
3. gradle下載及安裝
(1).官網下載地址:
(2).檢視原始碼framework-bom下的gradle下的wrapper的gradle-wrapper.properties檔案,檢視distributionUrl的版本,然後在官網上對應下載all的zip檔案,以便出錯可以看其原始碼問題.
(3).安裝成功後檢視版本.
執行
1 gradle -v
View Code
如上圖表示安裝成功.
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 徑地址)View Code
6、修改全域性(根目錄下的)build.gradle檔案(這個就相當於是maven的pom檔案),在檔案頭部加上。
(1)、新增如下:
1 buildscript { 2 repositories { 3 // 新的spring庫 4 maven { url "https://repo.spring.io/plugins-release" } 5 } 6 }View Code
(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 }View Code
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 }View Code
(2)settings.gradle內容(如果沒有檔案自行新增)
1 rootProject.name = 'spring-2020test'
View Code
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 }View Code
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 }View Code
(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 }View Code
(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 }View Code
(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:解決不同模組依賴版本不同問題