Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 968
Guru Avatar asked Feb 04 '19 15:02

Guru


3 Answers

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

like image 32
shasr Avatar answered Nov 07 '22 18:11

shasr


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.

like image 34
veben Avatar answered Nov 07 '22 20:11

veben


You can use it with active profile

@Configuration
@PropertySource("classpath:my-custom-${spring.profiles.active}.properties")
like image 168
Irosh Kuruppu Avatar answered Nov 07 '22 18:11

Irosh Kuruppu