Spring Boot - 應用屬性



應用屬性支援我們在不同的環境中工作。在本章中,您將學習如何為 Spring Boot 應用程式配置和指定屬性。

命令列屬性

Spring Boot 應用程式將命令列屬性轉換為 Spring Boot 環境屬性。命令列屬性優先於其他屬性源。預設情況下,Spring Boot 使用 8080 埠號啟動 Tomcat。讓我們學習如何使用命令列屬性更改埠號。

步驟 1 - 建立可執行 JAR 檔案後,使用命令java –jar <JARFILE>執行它。

步驟 2 - 使用下面截圖中給出的命令,透過使用命令列屬性更改 Spring Boot 應用程式的埠號。

Command Line Properties JARFILE

注意 - 您可以使用分隔符 - 提供多個應用程式屬性。

屬性檔案

屬性檔案用於在一個檔案中儲存“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

Externalized 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 活動配置檔案的命令如下所示 -

Prod.Properties Active Dev

您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -

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 活動配置檔案,如下所示 -

Production Active Profile

您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -

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

設定開發活動配置檔案的命令如下所示 -

Prod.Properties Active Dev

您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -

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 活動配置檔案的命令如下所示 -

Production Active Profile

您可以在控制檯日誌中看到活動配置檔名稱,如下所示 -

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)
廣告

© . All rights reserved.