一、Spring整體架構和環境搭建
1.1 Spring的整體架構
Spring框架是一個分層架構,包含一系列功能要素,被分為大約20個模塊

- Spring核心容器:包含Core、Bean、Context、Expression Language模塊
- Core :其他組件的基本核心,主要包含Spring框架基本的核心工具類
- Beans :包含訪問配置文件、創建和管理bean以及IOC/DI(控制反轉和依賴注入)操作相關的所有類
- Context :構建與Core和Beans基礎之上,繼承BeanFactory,提供上下文信息,擴展出JNDI、EJB、電子郵件、國際化等功能
- Expression Language :提供了一個強大的語言表達式用于在運行時查詢和操縱對象。
- Spring數據訪問與集成
- JDBC :提供了JDBC的抽象層
- ORM :提供了JPA、JDO、Hibernate、iBatis 等ORM映射層
- OXM :提供了Object/XML映射實現的抽象層,該實現包括JAXB、Castor、XMLBeans、JiBX和XStream
- JMS(Java Messaging Service) :制造和消費消息
- Transaction :編程和聲明性的事務管理
- Spring AOP:集成了所有AOP功能
- Spring Web與遠程調用
- Web :提供了基礎的 Web 開發的上下文信息,現有的Web框架,如JSF、Tapestry、Structs等,提供了集成
- Web MVC:提供了 Web 應用的 Model-View-Controller 全功能實現。
- Websocket
1.2 Spring環境搭建
1.2.1 安裝git
Git - Downloads
1.2.2 安裝Gradle
1.)下載安裝包
下載地址:? Gradle Distributions
2)配置環境變量
#1.打開.bash_profile文件
open -e ~/.bash_profile
#2.在.bash_profile文件中配置環境變量
GRADLE_HOME=//Users/sunshine/Documents/software/gradle/gradle-7.5.1
export GRADLE_HOME
export PATH=$PATH:$GRADLE_HOME/bin
3)查看Gradle版本
gradle -version
1.2.3 下載Spring源碼
git clone git@github.com:spring-projects/spring-framework.git
git clone https://github.com/spring-projects/spring-framework.git
我使用的是5.3.x分支。
注意:要使用 git@github.com:spring-projects/spring-framework.git 需要配置ssh秘鑰:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
//會生成私鑰文件 id_rsa 和公鑰文件 id_rsa.pub
//windows 一般在 C:\Users\Administrator\.ssh 目錄,linux和mac在 ~/.ssh
//把生成的公鑰拷貝到GitHub 的設置中的 SSH 密鑰部分
如果使用 https 超時,可以增加 Git 的 HTTP/HTTPS 超時時間:
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
如果使用了VPN代理服務器,確保 Git 的代理設置是正確的。你可以通過以下命令檢查或設置代理:
//查看當前代理設置
git config --global --get http.proxy
//設置代理
git config --global http.proxy http://proxy_host:port
git config --global https.proxy https://proxy_host:port
//取消代理設置
git config --global --unset http.proxy
git config --global --unset https.proxy
1.2.4 配置IDEA
配置IDEA的本地gradle環境:
配置項目字節碼版本:
最后在 Project Structure 配置sdk版本。
1.2.5 配置 Spring-Framework 源碼的gradle倉庫
配置gradle下載地址為本地(路徑為gradle二進制文件壓縮包路徑):
//文件為 gradle/wrapper/gradle-wrapper.properties
distributionUrl=file:///F:/work_folder/gradle-7.5.1-bin.zip
配置倉庫鏡像 :
//根目錄下文件\buildSrc\build.gradle
repositories {maven { url 'https://maven.aliyun.com/repository/public/' }maven { url 'https://maven.aliyun.com/repository/central' }mavenCentral()gradlePluginPortal()
}
根目錄下文件\build.gradle 的 mavenCentral() 之前加上:
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url "https://maven.aliyun.com/repository/spring-plugin" }
maven { url "https://maven.aliyun.com/repository/gradle-plugin" }
1.2.6 新建模塊,測試環境
添加依賴 /spring-f2-test/build.gradle :
添加測試類:
public class User {String name;public User(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +'}';}
}@Configuration
public class JavaConfig {@Beanpublic User user(){return new User("lister");}
}public class TestApplication {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);User user = (User) context.getBean("user");System.out.println(user);}
}
//結果打印
User{name='lister'}