- Spring Boot 教程
- Spring Boot - 首頁
- Spring Boot - 簡介
- Spring Boot - 快速入門
- Spring Boot - 引導
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 構建系統
- Spring Boot - 程式碼結構
- Spring Bean & 依賴注入
- Spring Boot - 執行器
- Spring Boot - 啟動器
- Spring Boot - 應用屬性
- Spring Boot - 配置
- Spring Boot - 註解
- Spring Boot - 日誌
- 構建 RESTful Web 服務
- Spring Boot - 異常處理
- Spring Boot - 攔截器
- Spring Boot - Servlet 過濾器
- Spring Boot - Tomcat 埠號
- Spring Boot - Rest 模板
- Spring Boot - 檔案處理
- Spring Boot - 服務元件
- Spring Boot - Thymeleaf
- 消費 RESTful Web 服務
- Spring Boot - CORS 支援
- Spring Boot - 國際化
- Spring Boot - 排程
- Spring Boot - 啟用 HTTPS
- Spring Boot - Eureka 伺服器
- 使用 Eureka 註冊服務
- 閘道器代理伺服器和路由
- Spring Cloud 配置伺服器
- Spring Cloud 配置客戶端
- Spring Boot - 執行器
- Spring Boot - Admin 伺服器
- Spring Boot - Admin 客戶端
- Spring Boot - 啟用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 建立 Docker 映象
- 跟蹤微服務日誌
- Spring Boot - Flyway 資料庫
- Spring Boot - 傳送電子郵件
- Spring Boot - Hystrix
- Spring Boot - WebSocket
- Spring Boot - 批處理服務
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 單元測試用例
- Rest 控制器單元測試
- Spring Boot - 資料庫處理
- 保護 Web 應用程式
- Spring Boot - 使用 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登入
- Spring Boot 資源
- Spring Boot - 快速指南
- Spring Boot - 有用資源
- Spring Boot - 討論
Spring Boot - 應用屬性
應用屬性支援我們在不同的環境中工作。在本章中,您將學習如何為 Spring Boot 應用程式配置和指定屬性。
命令列屬性
Spring Boot 應用程式將命令列屬性轉換為 Spring Boot 環境屬性。命令列屬性優先於其他屬性源。預設情況下,Spring Boot 使用 8080 埠號啟動 Tomcat。讓我們學習如何使用命令列屬性更改埠號。
步驟 1 - 建立可執行 JAR 檔案後,使用命令java –jar <JARFILE>執行它。
步驟 2 - 使用下面截圖中給出的命令,透過使用命令列屬性更改 Spring Boot 應用程式的埠號。
注意 - 您可以使用分隔符 - 提供多個應用程式屬性。
屬性檔案
屬性檔案用於在一個檔案中儲存“N”個屬性,以便在不同的環境中執行應用程式。在 Spring Boot 中,屬性儲存在類路徑下的application.properties檔案中。
application.properties 檔案位於src/main/resources目錄中。下面給出了示例application.properties檔案的程式碼 -
server.port = 9090 spring.application.name = demoservice
請注意,在上圖所示的程式碼中,Spring Boot 應用程式 demoservice 從埠 9090 開始。
YAML 檔案
Spring Boot 支援基於 YAML 的屬性配置來執行應用程式。我們可以使用application.yml檔案代替application.properties檔案。此 YAML 檔案也應該儲存在類路徑中。下面給出了示例application.yml檔案 -
spring:
application:
name: demoservice
server:
port: 9090
外部化屬性
我們可以將屬性儲存在不同的位置或路徑中,而不是將屬性檔案儲存在類路徑下。在執行 JAR 檔案時,我們可以指定屬性檔案路徑。您可以使用以下命令在執行 JAR 時指定屬性檔案的位置 -
-Dspring.config.location = C:\application.properties
使用 @Value 註解
@Value 註解用於在 Java 程式碼中讀取環境或應用程式屬性值。讀取屬性值的語法如下所示 -
@Value("${property_key_name}")
請檢視以下示例,該示例顯示瞭如何在 Java 變數中使用 @Value 註解讀取spring.application.name屬性值。
@Value("${spring.application.name}")
觀察下面給出的程式碼以更好地理解 -
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
@Value("${spring.application.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping(value = "/")
public String name() {
return name;
}
}
注意 - 如果在執行應用程式時找不到該屬性,Spring Boot 會丟擲 Illegal Argument 異常,如Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}"。
為了解決佔位符問題,我們可以使用下面給出的語法設定屬性的預設值 -
@Value("${property_key_name:default_value}")
@Value("${spring.application.name:demoservice}")
Spring Boot 活動配置檔案
Spring Boot 支援基於 Spring 活動配置檔案的不同屬性。例如,我們可以為開發和生產保留兩個單獨的檔案來執行 Spring Boot 應用程式。
Spring 活動配置檔案在 application.properties 中
讓我們瞭解如何在 application.properties 中使用 Spring 活動配置檔案。預設情況下,application.properties 將用於執行 Spring Boot 應用程式。如果您想使用基於配置檔案的屬性,我們可以為每個配置檔案保留單獨的屬性檔案,如下所示 -
application.properties
server.port = 8080 spring.application.name = demoservice
application-dev.properties
server.port = 9090 spring.application.name = demoservice
application-prod.properties
server.port = 4431 spring.application.name = demoservice
在執行 JAR 檔案時,我們需要根據每個屬性檔案指定 Spring 活動配置檔案。預設情況下,Spring Boot 應用程式使用 application.properties 檔案。設定 Spring 活動配置檔案的命令如下所示 -
您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -
2024-09-04 08:13:16.322 INFO 14028 --- [ main] com.tutorialspoint.demo.DemoApplication : The following profiles are active: dev
現在,Tomcat 已在埠 9090 (http) 上啟動,如下所示 -
2024-09-04 08:13:20.185 INFO 14028 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)
您可以設定 Production 活動配置檔案,如下所示 -
您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -
2024-09-04 08:13:16.322 INFO 14028 --- [ main] com.tutorialspoint.demo.DemoApplication : The following profiles are active: prod
現在,Tomcat 在埠 4431 (http) 上啟動,如下所示 -
2024-09-04 08:13:20.185 INFO 14028 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 4431 (http)
Spring 活動配置檔案用於 application.yml
讓我們瞭解如何在 application.yml 中保留 Spring 活動配置檔案。我們可以將 Spring 活動配置檔案屬性儲存在單個application.yml檔案中。無需像 application.properties 一樣使用單獨的檔案。
以下是如何在 application.yml 檔案中保留 Spring 活動配置檔案的示例程式碼。請注意,分隔符 (---) 用於分隔 application.yml 檔案中的每個配置檔案。
spring:
application:
name: demoservice
server:
port: 8080
---
spring:
profiles: dev
application:
name: demoservice
server:
port: 9090
---
spring:
profiles: prod
application:
name: demoservice
server:
port: 4431
設定開發活動配置檔案的命令如下所示 -
您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -
2024-09-04 08:41:37.202 INFO 14104 --- [ main] com.tutorialspoint.demo.DemoApplication : The following profiles are active: dev
現在,Tomcat 在埠 9090 (http) 上啟動,如下所示 -
2024-09-04 08:41:46.650 INFO 14104 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)
設定 Production 活動配置檔案的命令如下所示 -
您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -
2024-09-04 08:43:10.743 INFO 13400 --- [ main] com.tutorialspoint.demo.DemoApplication : The following profiles are active: prod
這將使 Tomcat 在埠 4431 (http) 上啟動,如下所示
2024-09-04 08:43:14.473 INFO 13400 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 4431 (http)