简体   繁体   English

在 Spring 引导中配置特定的自定义属性文件

[英]Profile specific custom property files in Spring boot

Wanted to check if Spring boot offers help to use configuration file apart from application.properties file.想要检查 Spring 引导是否提供帮助以使用除application.properties文件之外的配置文件。 Ex: my-custom.properties file that can be profile specific, ex:例如:可以是特定于配置文件的my-custom.properties文件,例如:

  1. my-custom-dev.properties for dev profile开发配置文件的my-custom-dev.properties
  2. my-custom-uat.properties for uat profile my-custom-uat.properties配置文件的my-custom-uat.properties

Edit: The question is that, I have the normal application-{env}.property file, apart from that, there are other property files in accord to their data content (ex: DB specific properties for logging, that I want to store in `db-log.properties, How would I make the other files profile sensitive?编辑:问题是,我有普通的 application-{env}.property 文件,除此之外,还有其他根据其数据内容的属性文件(例如:我想存储在数据库中的日志记录特定属性`db-log.properties,如何使其他文件的配置文件敏感?

You can use it with active profile您可以将它与活动配置文件一起使用

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

In addition to application.properties files,除了 application.properties 文件,

profile-specific properties can be defined with following convention: application-{profile}.properties.可以使用以下约定定义特定于配置文件的属性:application-{profile}.properties。

The Environment has a set of default profiles (by default, [default]) that are used if no active profiles are set(In other words, if no profiles are explicitly activated, then properties from application-default.properties are loaded)环境有一组默认配置文件(默认情况下,[默认]),如果没有设置活动配置文件(换句话说,如果没有明确激活配置文件,则加载来自 application-default.properties 的属性)

To run multiple profiles:要运行多个配置文件:

1.application-prod.properties 1.application-prod.properties

2.application-dev.properties 2.application-dev.properties

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

3.application.properties (Default profile) 3.application.properties(默认配置文件)

mvn spring-boot:run

4.Command Line Args with custom property files 4.带有自定义属性文件的命令行参数

spring.config.name - Set configuration files names(comma separated values) spring.config.location - Set the locations where Spring Boot will find your externalized configuration files. 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-profiles.html

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

Additionaly to the application.properties file, you can define as application-{profile}.properties as you want.除了application.properties文件之外,您还可以根据需要将其定义为application-{profile}.properties The chosen file is determinated at launch, following the profile you have selected.所选文件在启动时根据您选择的配置文件确定。

Yes.是的。 Absolutely.绝对地。 You just need to provide the profile you'd like when starting.您只需要在开始时提供您想要的配置文件。
eg:例如:

-Dspring.profiles.active=prod 

will use application-prod.properties将使用 application-prod.properties

-Dspring.profiles.active=customer

will use application-custom.properties将使用 application-custom.properties

The default convention used by spring is application-.properties. spring 使用的默认约定是 application-.properties。 So if profile is dev then it looks for application-dev.properties所以如果 profile 是 dev 那么它会寻找 application-dev.properties

You al can also use bootstrap.properties file and can specify spring.application.name=my-custom您也可以使用 bootstrap.properties 文件并可以指定 spring.application.name=my-custom

In that case spring will look for my-custom.properties file and of course u can use it with profile dev,uat so the property file name should be my-custom-dev.properties.在这种情况下,spring 将查找 my-custom.properties 文件,当然您可以将它与配置文件 dev,uat 一起使用,因此属性文件名应该是 my-custom-dev.properties。

Also u can pass configuration files as command line arguments as well -Dspring.config.location=path of the file.您也可以将配置文件作为命令行参数以及 -Dspring.config.location=path 文件传递​​。

Yes, Spring Boot helps you with that.是的,Spring Boot 可以帮助您。

Let Spring manage different property files for you by using spring.config.import property as in:让 Spring 使用 spring.config.import 属性为您管理不同的属性文件,如下所示:

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

Assuming my-custom.properties file is in same directory as application.properties.假设 my-custom.properties 文件与 application.properties 位于同一目录中。 This way Spring will also be able to manage different profiles for your custom properties files, that is, if you have 'dev' profile active, my-custom-dev.properties will be loaded for you.这样 Spring 也将能够为您的自定义属性文件管理不同的配置文件,也就是说,如果您激活了“dev”配置文件,将为您加载 my-custom-dev.properties。

See the docs .请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM