簡體   English   中英

如何在 Spring-Boot 的生產過程中覆蓋 application.properties?

[英]How to override application.properties during production in Spring-Boot?

我正在使用 spring boot 和application.properties在開發過程中通過@Configuration @Profile("dev")選擇一個數據庫。

spring.profiles.active=dev
spring.config.location=file:d:/application.properties

在生產期間,我想在應用程序上下文之外創建一個文件,該文件應該被加載,然后使用 d:/application.properties 激活不同的配置文件:

spring.profiles.active=production

結果:當我啟動應用程序時,配置仍然是dev ,所以不知何故不考慮生產屬性文件的附加位置。 我錯過了什么嗎?

彈簧靴 1.1.0.BUILD-SNAPSHOT

注意:這個問題是不是tomcat的

我知道你問過如何做到這一點,但答案是你不應該這樣做。

相反,有一個application.propertiesapplication-default.properties application-dev.properties等,並通過 args 將配置文件切換到 JVM:例如-Dspring.profiles.active=dev

您還可以在測試時使用@TestPropertySource覆蓋某些內容

理想情況下,一切都應該在源代碼控制中,這樣就不會出現意外,例如,您如何知道您的服務器位置中有哪些屬性,哪些屬性丟失了? 如果開發人員引入新事物會怎樣?

Spring Boot 已經為您提供了足夠的方法來做到這一點。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

您還可以使用@PropertySources

@PropertySources({
        @PropertySource(value = "classpath:application.properties"),
        @PropertySource(value = "file:/user/home/external.properties", ignoreResourceNotFound = true)
})
public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

    }


}

我不確定您是否可以動態更改配置文件。

為什么不只是將spring.config.location屬性設置為所需的外部位置的內部屬性文件,並且該位置(jar 外部)的屬性文件設置spring.profiles.active屬性?

更好的是,有一個內部屬性文件,特定於開發配置文件(具有 spring.profiles.active=dev)並保持原樣,當您想在生產中部署時,為您的屬性文件指定一個新位置,其中包含 spring .profiles.active=產品:

java -jar myjar.jar --spring.config.location=D:\wherever\application.properties

從 Spring Boot 2 開始,您將不得不使用

--spring.config.additional-location=production.properties

更新:這是春天的一個錯誤,見這里

jar 之外的應用程序屬性必須位於以下位置之一,然后一切正常。

21.2 Application property files
SpringApplication will load properties from application.properties files in the following    locations and add them to the Spring Environment:

A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root

因此,例如,當您不想指定 cmd 行參數並且不在基本 app.props 中使用 spring.config.location 時,這應該可以工作:

d:\yourExecutable.jar
d:\application.properties

or

d:\yourExecutable.jar
d:\config\application.properties

請參閱spring 外部配置文檔

更新:您可以將 \\@Configuration 與 \\@PropertySource 一起使用。 根據此處的文檔您可以在任何地方指定資源。 您應該小心,何時加載哪個配置以確保您的產品獲勝。

我發現以下內容對我有用:

java -jar my-awesome-java-prog.jar --spring.config.location=file:/path-to-config-dir/

file:添加。

后期編輯

當然,這個命令行永遠不會像在生產中那樣運行。

而是我有

  • [可能有幾層] 源代碼控制中的shell腳本,占位符用於可能更改的命令的所有部分(jar 的名稱、配置路徑...)
  • ansible部署腳本將部署shell腳本並用實際值替換占位符。

使用 Spring Boot 2.2.2.Release 進行更新。

完整示例在這里,https://www.surasint.com/spring-boot-override-property-example/

假設,在您的 jar 文件中,您的 application.properties 包含以下兩行:

server.servlet.context-path=/test
server.port=8081

然后,在生產中,您希望覆蓋 server.port=8888 但您不想覆蓋其他屬性。

首先,您創建另一個文件,例如 override.properties 並在線設置這一行:

server.port=8888

然后你可以像這樣啟動jar

java -jar spring-boot-1.0-SNAPSHOT.jar --spring.config.location=classpath:application.properties,/opt/somewhere/override.properties

spring 配置優先級如下。

  1. ServletConfig 初始化參數
  2. ServletContext 初始化參數
  3. JNDI 屬性
  4. System.getProperties()

因此,如果您希望這樣做,您的配置將在命令行中被覆蓋。 但建議避免覆蓋,盡管您可以使用多個配置文件。

暫無
暫無

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

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