Spring boot
什麼是 Spring Boot ?
大致包含三大要素
- starter 整合程式庫 :
不用一一找版本加入(利用 Maven 功能整合依賴項目),提供很多 starter,開發者可依需求選用。 - 自動組態(Auto Configuration) :
EX.使用啟動器 spring-boot-starter-web,預設會註冊 DispatcherServlet、託管 View 解析器(InternalResourceViewResolver) - 內嵌執行環境(embedded Web Server) :
不需要額外的 web server
EX.使用啟動器 spring-boot-starter-web,預設會自帶 Tomcat
另外提供 CLI(Command Line Interface )及執行器(Actuator)。
**注 :
CLI 是 Command Line Interface 的縮寫,中文為命令列面,是一種文字化的使用者介面。通常是透過終端(Terminal)或命令提示字元(Command Prompt)等軟體,鍵盤輸入指令,並以文字的方式呈現結果。CLI 可以用來控制腦、操作檔案、執行程式等,並且通常是自由軟體的一部分。
相對於圖形使用者介面(GUI),CLI 的優勢在於其快速、靈和可編程的特性。使用者可以透過鍵盤輸入指令,不需要透過鼠點選圖形介面上的按鈕,這可以提高操作速度和效率。同時CLI 也提供了豐富的指令選 項和參數,可以透過組合指令參數實現更加複雜的操作。
常見的 CLI 包括 Unix/Linux 系統中的 shell,以及Windows 系統中的命令提示字元(Command Prompt)和Windows PowerShell 等。除了系統自帶的 CLI,還有許多第方的 CLI 工具和腳本語言,例如 Git、Docker、Python 等這些工具可以透過命令行實現自動化操作和快速部署。
約定優於組態(Convention over Configuration)
資料庫 table 名為 Member,基於 ORM 對應類別名應該也要為 Member
如果跟約定的不一樣,需要額外做映射。
另外,此將開發者們常用的組態設定視為約定,如不合用就另外自行設定。
Spring Boot 的精神為,讓開發者做最少的前置作業,即可開始開發。
starter 機制
Spring Boot 利用 Maven 的依賴/繼承,定義幾個模塊
- spring-boot-dependencies
使用 Maven 的標籤,封裝所有 Spring 系列框架,⼀般不會直接使用,僅當作 spring-boot-starter-parent 的父模塊 - spring-boot-starter-parent
設定所有 Spring Boot Starter 專案所需的 Maven 專案相關設定
⼀般不會直接使用,僅當作自建的 Spring Boot Starter 專案之⽗模塊 - spring-boot-starter-*
封裝針對某功能相關的 Spring 系列框架的程式庫,會直接使用,當作自建的 Spring Boot Starter 專案之依賴
EX. spring-boot-starter-web,封裝了以下程式庫- spring-web
- spring-webmvc
- spring-boot-starter-json
- spring-boot-starter-tomcat
自建的 Spring Boot Starter 專案。。。
- 靠間接繼承 spring-boot-dependencies,處理程式庫間版本支援
- 靠繼承 spring-boot-starter-parent,獲得 Maven 專案相關設定
- 靠使用 spring-boot-starter-*,加入 Spring 系列框架程式庫
starter 可分成 3 個大類 :
- application : 應用程式類
- production : 正式環境類
- technical : 系統層級類
常見啟動器詳見講義。
封裝成 jar 與 war 的差別
Spring Boot 用非 web 專案就可以執行,如果想要放到另外一台 application Server 上執行,或是要使用 JSP,才需要包成 war,如想要直接下指令 run 可以包成 jar 即可。
Spring Boot Starter 專案
針對專案結構做說明
預設會建立 專案名 Application.java,是 Spring Boot Starter 專案得主類別,裡面有 main 方法,為執行的進入點。
其特徵為@SpringBootApplication,以及呼叫 SpringApplication.run()。
@SpringBootApplication 包含 3 個 Annotation :
- @SpringBootConfiguration : 設定為 Spring Boot 核心組態類別,等同於非 Spring Boot 中的@Configuration
- @EnableAutoConfiguration : 啟用自動組態
- @ComponentScan : 指定掃描 Bean 元件,預設掃描套件為新建 Spring Boot Starter 專案時,設定的根套件名(Package)
SpringApplication.run() 為 Spring Boot 環境的程式進入點
1 |
|
如果新建時打包成 war,會生成 ServletInitializer.java,為部署描述類別,是 web.xml 的取代品
資源目錄
這裡比較特別要注意的是,靜態資源的位置有較大的變化。在 src/main/resources 下,有 static 和 templates 目錄。
- static : 為 Web 靜態資源目錄,通常放.js .css .img 之類的檔案,jsp 放置此處毫無作用
- templates : 為 Thymeleaf 專用資源目錄(Spring Boot 推薦在 Spring MVC 的 View 層使用 Thymeleaf)
- application.properties : Spring Boot 核心組態檔
測試程式目錄 src/test/java
預設會建立,測試程式放此
Web 根目錄(webapp)
打包方式選 war 才會建立,即 Java web 的 webapp 目錄,若需要在 Spring Boot 中使用 JSP,須放至此目錄(需 WEB-INF ⽬錄,則須手動建⽴)
靜態資源管理機制
當客戶端對本網站某 URI 請求靜態資源,Spring Boot 會依順序尋找
- classpath:/META-INF/resources/ (須手動建立)
- classpath:/resources/
- classpath:/static/
- classpath:/public/ (須手動建立)
找到 → 回應;找不到 → 下⼀個目錄
Spring Boot 預設首頁檔名是 index.xml,會依照順序在靜態資源目錄尋找。
若想要自訂則需要使用 Spring MVC 組態設定。
Auto Configuration
- 所有的組態設定預設都在 spring-boot-autoconfigure-x.y.z.jar
- 開發者透過 starter 決定使用哪些預設
舉例常用的 2 個 :
spring-boot-starter-data-jpa
- 託管 DataSource(連線自行決定)
- 託管 TransactionManager(啟用交易控制)
- 底層為 Hibernate
spring-boot-starter-web
- 託管 ViewResolver
- 設定靜態資源管理
- 託管 MessageConverter
- 設定預設⾸⾴
核心組態檔
只有當預設值不合用,或是無預設值時才會再次設定
支援 2 種格式,分別為常數檔 properties(default)以及 yml。
路徑為 src/main/resources(預設),檔名為 application.properties/application.yml
組態內容 : 屬性名均為小寫,且使用 Spring Tools 4 會有提示字。
若 2 者皆存在,以 properties 優先。
YAML
- YAML Ain’t Markup Language(YAML 不是⼀種標記語⾔)
- ⼀種易讀易寫的資料格式,其格式中的空⽩、縮排、換行,有特殊意義
- 副檔名可為 yaml yml
語法為 key : value,如換行再縮排,表示此屬性在前⼀個未縮排的屬性之命名空間下。
值的語法 :
- 字面常數(Literal)
- 物件(Key Value pair)
- 陣列
1 | # properties |
yml 冒號後面要空白
1 | # yml |
常用組態詳見講義
內嵌 application web server
共支援 3 種內嵌伺服器
Tomcat、Jetty、Underrtow
Spring Boot 的內嵌式 Web 伺服器是預設啟動的,所以如果要丟到外部 Web 伺服器,需要在 pom.xml 設定排除 Tomcat 啟動器,不然會發生端口(port)衝突。
1 | <dependency> |





