簡體   English   中英

Spring命令行中的多個外部屬性文件

[英]Multiple external property file in spring boot from command line

我想從外部位置讀取2個屬性文件。 它需要通過命令行參數加載。

  1. configuration.properties - >這是一個普通的屬性文件,包含公共信息。

  2. secure.properties - >這包含加密密碼。

我給出以下命令行參數

-Dspring.config.location=file:C:\Project\properties\configuration.properties, file:C:\Project\properties\security\dev\secure.properties

configuration.properties的屬性工作正常。 因為我不是直接加載文件,而是使用屬性。

在這里,對於這個加密文件,我需要傳遞路徑並明確加載,這不是發生的。

此外,util EncryptedPropertiesReader在jar中可用,因此無法修改它。

這里,我需要使用secure.properties

我們有一個類EncryptedPropertiesReader 它包含一個load(string path)方法。

因此,我需要傳遞secure.properties文件的路徑。

String env = "dev" 
Properties p = EncryptedPropertiesReader.load("security\" + env + "\secure.properties");
System.out.println(p); // null

在這里,我不能給出絕對路徑,因為在不同的環境(Unix)中,我會有不同的路徑。

因此命令行是我的選擇,需要保持外部屬性。

這是我試過的組合:


  • 命令行 :

    1. 為安全提供classpath。 屬性

       -Dspring.config.location=file:C:\\Project\\properties\\configuration.properties, classpath:C:\\Project\\properties\\security\\dev\\ 
    2. spring.config.nane

       -D spring.config.name=configuration, secure - Dspring.config.location=file:C:\\Project\\properties\\configuration.properties, file:C:\\Project\\properties\\security\\dev\\secure.properties 
    3. 試圖改變\\到/


  • 網址的路徑通過

    1. ("secure.properties"); //當我加載類路徑時,期望工作

    2. ("/secure.properties"); //當我加載類路徑時,期望工作


上述組合均無效。

知道出了什么問題嗎? 或者我錯過了什么?

為長期問題道歉。

24.3應用程序屬性文件

SpringApplication從以下位置的application.properties文件加載屬性,並將它們添加到Spring環境中:

  1. 當前目錄的A / config子目錄
  2. 當前目錄
  3. 一個classpath / config包
  4. 類路徑根

列表按優先級排序(在列表中較高位置定義的屬性將覆蓋在較低位置中定義的屬性)。

[注意]您也可以使用YAML('.yml')文件替代'.properties'。

如果您不喜歡application.properties作為配置文件名,則可以通過指定spring.config.name環境屬性來切換到另一個文件名。 您還可以使用spring.config.location環境屬性(以逗號分隔的目錄位置或文件路徑列表)來引用顯式位置。

以下示例顯示如何指定其他文件名:

$ java -jar myproject.jar --spring.config.name=myproject

以下示例顯示如何指定兩個位置:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

[警告] spring.config.name和spring.config.location很早就用來確定哪些文件必須加載,因此必須將它們定義為環境屬性(通常是OS環境變量,系統屬性或命令) -line參數)。

如果spring.config.location包含目錄(而不是文件),則它們應該以/結尾(並且在運行時,在加載之前附加從spring.config.name生成的名稱,包括特定於配置文件的文件名)。 spring.config.location中指定的文件按原樣使用,不支持特定於配置文件的變體,並且被任何特定於配置文件的屬性覆蓋。

以相反的順序搜索配置位置。 默認情況下,配置的位置是

classpath:/,classpath:/config/,file:./,file:./config/.

生成的搜索順序如下:

file:./config/ file:./ classpath:/config/ classpath:/

使用spring.config.location配置自定義配置位置時,它們將替換默認位置。 例如,如果spring.config.location配置了值classpath:/ custom-config /,file:./ custom-config /,搜索順序將變為以下內容:

file:./custom-config/ classpath:custom-config/

或者,當使用spring.config.additional-location配置自定義配置位置時,除默認位置外,還會使用它們。 在默認位置之前搜索其他位置。 例如,如果配置了classpath:/ custom-config /,file:./ custom-config /的其他位置,則搜索順序將變為以下內容:

file:./custom-config/ classpath:custom-config/ file:./config/ file:./ classpath:/config/ classpath:/

此搜索順序允許您在一個配置文件中指定默認值,然后有選擇地覆蓋另一個配置文件中的值。 您可以在其中一個默認位置的application.properties(或使用spring.config.name選擇的任何其他基本名稱)中為應用程序提供默認值。 然后,可以在運行時使用位於其中一個自定義位置的不同文件覆蓋這些默認值。

[Note]如果使用環境變量而不是系統屬性,大多數操作系統都不允許使用句點分隔的鍵名,但您可以使用下划線(例如,使用SPRING_CONFIG_NAME而不是spring.config.name)。

[Note]如果應用程序在容器中運行,則可以使用JNDI屬性(在java:comp / env中)或servlet上下文初始化參數來代替環境變量或系統屬性。

這就是你可以使用環境變量從任何位置加載屬性的方法

-Dspring.config.location="C:\Project\properties\", -Dsecure.properties.location="C:\Project\properties\security\dev\"




 @PropertySources({ 
             @PropertySource("file:${spring.config.location}/configuration.properties"),
             @PropertySource("file:${secure.properties.location}/secure.properties")})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM