10

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?

Guru
  • 2,739
  • 1
  • 25
  • 27
  • 2
    That is explained [here](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files) in the Spring Boot Reference Guide. In short add `--spring.config.name=application,file1,file2` this will instruct Spirng Boot to (optionally) detect `file1.properties` and `file1-{profile}.properties next to the default ones. – M. Deinum Jul 01 '19 at 08:27
  • I add spring.config.import in a application.properties to point to file1.yaml. However, when i activate dev profile, it does not look for file1-dev.yaml. This shows that auto appending profile to custom yaml name won't work. Am I right? – Nick Wills Feb 18 '22 at 07:03

6 Answers6

6

You can use it with active profile

@Configuration
@PropertySource("classpath:my-custom-${spring.profiles.active}.properties")
Irosh Kuruppu
  • 104
  • 1
  • 2
  • 3
    This won't work they way you think it works. `spring.profiles.active` is an array and not a single element. You can enable multiple profiles at a single time. Then this won't work as it will then look for a file named `my-custom-[profile1,profile2].properties`. Which I hardly imagine being the actual name. – M. Deinum Jul 01 '19 at 08:29
2

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

shasr
  • 325
  • 2
  • 5
  • when you activate multiple profiles this way, is the order of the properties files deterministic (in case you give the same property different values) ? – herman Aug 27 '19 at 08:24
1

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.

veben
  • 19,637
  • 14
  • 60
  • 80
0

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

MikeBFromPDX
  • 83
  • 1
  • 1
  • 8
0

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.

whysoseriousson
  • 196
  • 2
  • 16
0

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.

HFSDev
  • 417
  • 8
  • 15