Spring 原始碼環境搭建
Spring 是面向 Bean 的編程,Bean 在其中起到了巨大的作用,而 Spring 提供了 IOC 容器來管理物件之間的依賴關系,使我們可以更加關注業務,輕松的構建一個企業應用,借助 IOC 特性和 AOP 面向切面編程,可以說 Spring 為開發者提供了無限的可能,接下來我們一步步來揭穿其中的原理,就先從搭建原始碼開始吧!
1. 環境
- Gradle-5.6.4
- JDK-1.8
- IntelliJ IDEA 2022.3.2
- Spring-5.2.9RELEASE
- Windows 10
2. 構建工具 Gradle
通過官網的原始碼倉庫我們可以知道,Spring 官方構建工具是 Gradle,所以我們構建原始碼之前需要準備好 Gradle 環境,
2.1 Gradle 下載
Gradle 下載地址 :https://gradle.org/releases/
選擇 5.6.4 下載到本地(盡量使用一致的版本,避免編譯失敗部分組件無法下載),

2.2 Gradle 配置
- 配置 GRADLE_HOME:

- 配置 Path:

2.3 Gradle 驗證
執行 gradle -v 查看安裝情況:

出現上圖的輸出資訊,說明我們的環境準備就緒!
2.4 Gradle 倉庫
Gradle 倉庫一般都是國外的鏡像,為了加速我們的構建,我們將配置阿里鏡像倉庫進行加速,
- 在 Gradle 安裝目錄中的
init.d檔案夾下新建init.gradle檔案,配置阿里云加速器:
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/central'}
maven { url 'https://maven.aliyun.com/repository/jcenter'}
maven { url 'https://maven.aliyun.com/repository/public'}
maven { url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin'}
maven { url 'https://maven.aliyun.com/repository/grails-core'}
maven { url 'https://maven.aliyun.com/repository/spring'}
maven { url 'https://maven.aliyun.com/repository/spring-plugin'}
maven { url 'https://maven.aliyun.com/repository/apache-snapshots'}
mavenCentral()
}
}
3. 下載原始碼
本地我們搭建的環境選擇的原始碼是 Spring-5.2.9RELEASE 版本,盡量和我保持一致,避免出現其他的編譯問題,
3.1 Clone 代碼
spring-framework 倉庫地址:https://github.com/spring-projects/spring-framework
通過 git 命令將代碼拉下來:
# clone 代碼
git clone https://github.com/spring-projects/spring-framework.git
# 切 tag
git checkout Spring-5.2.9RELEASE -b Spring-5.2.9RELEASE
通過上的命令我們首先將代碼拉到本地,然后通過 git checkout 命令切換到指定的 tag 上,并且新建本地 Spring-5.2.9RELEASE 分支方便我們后續進行分支操作,
3.2 編譯代碼
在 spring-framework原始碼目錄中打開 cmd 命令列,執行 .\gradlew.bat 命令進行構建,該程序會持續一段時間,我們不妨去歇下來稍事等待,

出現 BUILD SUCCESSFUL 表示我們編譯程序結束了,接下來就可以匯入到 IDEA 中,
PS:
a. 我已經執行過一次編譯,所以我的時間是非常快的,
b. 若果編譯程序中出現了相關問題,需要根據報錯的資訊進行排查,
4. 匯入 IDEA
使用 IDEA 打開編譯好的專案,需要提前配置好 IDEA 的 Gradle 以及 JDK 版本,接下來等待 IDEA build 完成即可:


當出現如下資訊時,說明 IDEA 匯入構建完成:

那么接下來,我們就可以開始撰寫模塊開始驗證了,
5. 構建測驗模塊
為了閱讀原始碼,我們需要自己建立一個模塊用來測驗實體以及除錯原始碼,
5.1 新建 Module
選擇 Gradle 構建工具進行專案創建,完成后整體結構如下:



5.2 引入依賴
在 build.gradle 中共引入依賴,便于后續的測驗:
plugins {
id 'java'
}
group 'org.springframework'
version '5.2.9.RELEASE'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
testCompile group: 'junit', name: 'junit', version: '4.10'
compile(project(":spring-context"))
}
test {
useJUnitPlatform()
}
5.3 撰寫代碼
新建一個類用于測驗(加載到 IOC 容器中):
public class MyTestBean {
private String testStr = "testStr";
public String getTestStr() {
return testStr;
}
public void setTestStr(String testStr) {
this.testStr = testStr;
}
}
在 resources 目錄下新建 BeanFactoryTest.xml 檔案,配置 bean 定義:
<?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="myTestBean" />
</beans>
在 test 檔案夾中通過 junit 進行測驗:
public class BeanFactoryTests {
/**
* 手動加載 xml 獲取 bean
*/
@Test
public void simpleLoadTest() {
// 基于xml加載并注冊Bean的流程
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("BeanFactoryTest.xml"));
MyTestBean myTestBean = (MyTestBean) bf.getBean("myTestBean");
Assert.assertEquals("testStr", myTestBean.getTestStr());
}
@Test
public void testClassPathXmlApplication() {
// 基于 xml 加載并注冊 Bean 的流程
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:BeanFactoryTest.xml");
MyTestBean myTestBean = (MyTestBean) applicationContext.getBean("myTestBean");
Assert.assertEquals("testStr", myTestBean.getTestStr());
}
}
運行上述的其中一個測驗方法,如下圖沒有錯誤即表示我們的環境搭建完成:

本文來自博客園,作者:tianliangge,轉載請注明原文鏈接:https://www.cnblogs.com/tianliangge/p/17162345.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/545276.html
標籤:Java
上一篇:玩轉釘釘訊息推送!
