簡體   English   中英

使用外部屬性覆蓋 spring-boot application.properties

[英]Override spring-boot application.properties with external properties

我有一個 spring-boot 應用程序。 我有 3 個屬性文件:

  1. spring-boot jar 中的一個屬性文件 - myjar.jar稱為 application.properties (這是 jar 內的包

  2. jar 外部的屬性文件,位於configuration/global.properties下的jar 位置

  3. jar 外部的一個屬性文件,位於configuration/java.properties下的jar 位置

問題是,我正在運行以下命令:

java -Dlogging.config=logback.xml -jar "myjar.jar" spring.config.location=classpath:/application.properties,file:./configurations/global.properties,file:./configurations/java.properties

我得到一個例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'Data.StoresDb.LakeConnectionString' in value "${Data.StoresDb.LakeConnectionString}"
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
        at org.springframework.beans.fa

myApplication.java 中,我有:

@Value("${Data.StoresDb.LakeConnectionString}")
String dataLakeStoreDb;

在我application.properties沒有Data.StoresDb.LakeConnectionString,但有它在我的global.properties,我希望春天試圖SER值Data.StoresDb.LakeConnectionString之前解決所有文件

您將 arg/value 作為原始 Java 參數傳遞:

java -jar "myjar.jar" spring.config.location=...

該參數將出現在主類的String[] args中,但 Spring 環境不會意識到這一點。
您必須在它前面加上--以使參數成為 Spring 環境感知:

java -jar "myjar.jar" --spring.config.location=...

官方文檔

默認情況下,SpringApplication 會將任何命令行選項參數(以“--”開頭,例如 --server.port=9000)轉換為屬性並將其添加到 Spring Environment

或者作為替代將其作為系統屬性傳遞:

java -Dspring.config.location=... -jar "myjar.jar" 

作為旁注:注意spring.config.location會覆蓋默認位置,而 Spring Boot 2 中引入的spring.config.additional-location會將指定位置添加到默認位置。

暫無
暫無

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

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