简体   繁体   中英

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. Ex: my-custom.properties file that can be profile specific, ex:

  1. my-custom-dev.properties for dev profile
  2. my-custom-uat.properties for uat profile

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?

You can use it with active profile

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

In addition to application.properties files,

profile-specific properties can be defined with following convention: 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)

To run multiple profiles:

1.application-prod.properties

2.application-dev.properties

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

3.application.properties (Default profile)

mvn spring-boot:run

4.Command Line Args with custom property files

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.

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

Additionaly to the application.properties file, you can define as application-{profile}.properties as you want. 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

-Dspring.profiles.active=customer

will use application-custom.properties

The default convention used by spring is application-.properties. So if profile is dev then it looks for application-dev.properties

You al can also use bootstrap.properties file and can specify 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.

Also u can pass configuration files as command line arguments as well -Dspring.config.location=path of the file.

Yes, Spring Boot helps you with that.

Let Spring manage different property files for you by using spring.config.import property as in:

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

Assuming my-custom.properties file is in same directory as 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.

See the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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