簡體   English   中英

在 Spring 引導中配置特定的自定義屬性文件

[英]Profile specific custom property files in Spring boot

想要檢查 Spring 引導是否提供幫助以使用除application.properties文件之外的配置文件。 例如:可以是特定於配置文件的my-custom.properties文件,例如:

  1. 開發配置文件的my-custom-dev.properties
  2. my-custom-uat.properties配置文件的my-custom-uat.properties

編輯:問題是,我有普通的 application-{env}.property 文件,除此之外,還有其他根據其數據內容的屬性文件(例如:我想存儲在數據庫中的日志記錄特定屬性`db-log.properties,如何使其他文件的配置文件敏感?

您可以將它與活動配置文件一起使用

@Configuration
@PropertySource("classpath:my-custom-${spring.profiles.active}.properties")

除了 application.properties 文件,

可以使用以下約定定義特定於配置文件的屬性:application-{profile}.properties。

環境有一組默認配置文件(默認情況下,[默認]),如果沒有設置活動配置文件(換句話說,如果沒有明確激活配置文件,則加載來自 application-default.properties 的屬性)

要運行多個配置文件:

1.application-prod.properties

2.application-dev.properties

mvn spring-boot:run -Dspring-boot.run.profiles=dev,prod

3.application.properties(默認配置文件)

mvn spring-boot:run

4.帶有自定義屬性文件的命令行參數

spring.config.name - 設置配置文件名稱(逗號分隔值) spring.config.location - 設置 Spring Boot 將找到外部化配置文件的位置。

java -jar hello-world.jar --spring.config.name=application,conf --spring.config.location=classpath:/external/properties/,classpath:/com/learn/../../

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

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

除了application.properties文件之外,您還可以根據需要將其定義為application-{profile}.properties 所選文件在啟動時根據您選擇的配置文件確定。

是的。 絕對地。 您只需要在開始時提供您想要的配置文件。
例如:

-Dspring.profiles.active=prod 

將使用 application-prod.properties

-Dspring.profiles.active=customer

將使用 application-custom.properties

spring 使用的默認約定是 application-.properties。 所以如果 profile 是 dev 那么它會尋找 application-dev.properties

您也可以使用 bootstrap.properties 文件並可以指定 spring.application.name=my-custom

在這種情況下,spring 將查找 my-custom.properties 文件,當然您可以將它與配置文件 dev,uat 一起使用,因此屬性文件名應該是 my-custom-dev.properties。

您也可以將配置文件作為命令行參數以及 -Dspring.config.location=path 文件傳遞​​。

是的,Spring Boot 可以幫助您。

讓 Spring 使用 spring.config.import 屬性為您管理不同的屬性文件,如下所示:

#application.properties   
spring.config.import=./my-custom.properties

假設 my-custom.properties 文件與 application.properties 位於同一目錄中。 這樣 Spring 也將能夠為您的自定義屬性文件管理不同的配置文件,也就是說,如果您激活了“dev”配置文件,將為您加載 my-custom-dev.properties。

請參閱文檔

暫無
暫無

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

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