Spring Boot應用程式屬性

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

命令行屬性

Spring Boot應用程式將命令行屬性轉換為Spring Boot環境屬性。命令行屬性優先於其他屬性源。 默認情況下,Spring Boot使用8080端口號來啟動Tomcat。接下來將學習如何使用命令行屬性更改端口號。

步驟1 - 創建可執行JAR檔後,使用命令java -jar <JARFILE>運行它。
步驟2 - 使用下麵給出的螢幕截圖中給出的命令,使用命令行屬性更改Spring Boot應用程式的端口號。

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

屬性檔

屬性(properties)檔用於在單個檔中保留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}")

請看下麵的示例,它顯示了如何使用@Value批註讀取Java變數中的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將拋出非法參數異常,因為無法在值${spring.application.name}中解析占位符'spring.application.name'
要解決占位符問題,可以使用下麵給出的thr語法設置屬性的默認值 -

@Value("${property_key_name:default_value}")
@Value("${spring.application.name:demoservice}")

Spring Boot活動配置檔

Spring Boot支持基於Spring活動配置檔的不同屬性。 例如,可以保留兩個單獨的檔進行開發和生產,以運行Spring Boot應用程式。

application.properties中的Spring活動配置檔

下麵來瞭解如何在application.properties 中使用Spring活動配置檔。 默認情況下,application.屬性將用於運行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活動檔的命令如下所示 -

在控制臺日誌中看到活動的配置檔案名稱,如下所示 -

2017-11-26 08:13:16.322  INFO 14028 --- [
   main] com.zaixian.demo.DemoApplication  :
   The following profiles are active: dev

現在,Tomcat已經開始使用端口9090(http),如下所示 -

2017-11-26 08:13:20.185  INFO 14028 --- [
   main] s.b.c.e.t.TomcatEmbeddedServletContainer :
   Tomcat started on port(s): 9090 (http)

可以設置生產活動配置檔,如下所示 -

在控制臺日誌中看到活動的配置檔案名稱,如下所示 -

2017-11-26 08:13:16.322  INFO 14028 --- [
   main] com.zaixian.demo.DemoApplication  :
   The following profiles are active: prod

現在,Tomcat開始使用4431端口(http),如下所示 -

2017-11-26 08:13:20.185  INFO 14028 --- [
   main] s.b.c.e.t.TomcatEmbeddedServletContainer :
   Tomcat started on port(s): 4431 (http)

application.yml的Spring活動配置檔
下麵來瞭解如何為application.yml保留Spring活動配置檔。可以將Spring活動配置檔屬性保留在單個application.yml檔中。無需使用像application.properties這樣的單獨檔。
以下是將Spring活動配置檔保留在application.yml檔中的示例代碼。 請注意,分隔符號(---)用於分隔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

命令設置開發活動配置檔如下 -

在控制臺日誌中看到活動的配置檔案名稱,如下所示 -

2017-11-26 08:41:37.202  INFO 14104 --- [
   main] com.zaixian.demo.DemoApplication  :
   The following profiles are active: dev

現在,Tomcat開始使用端口9090(http),如下所示 -

2017-11-26 08:41:46.650  INFO 14104 --- [
   main] s.b.c.e.t.TomcatEmbeddedServletContainer :
   Tomcat started on port(s): 9090 (http)

設置生產活動配置檔的命令如下 -

在控制臺日誌中看到活動的配置檔案名稱,如下所示 -

2017-11-26 08:43:10.743  INFO 13400 --- [
   main] com.zaixian.demo.DemoApplication  :
   The following profiles are active: prod

這將在端口4431(http)上啟動Tomcat,如下所示:

2017-11-26 08:43:14.473  INFO 13400 --- [
   main] s.b.c.e.t.TomcatEmbeddedServletContainer :
   Tomcat started on port(s): 4431 (http)

上一篇: Spring Boot運行器(Runner) 下一篇: Spring Boot日誌