Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access application-{profile}.properties file based on active profile

I have a requirement of accessing application.properties file outside the project location. I am able to achieve the same using following :

@Component
@PropertySources({
        @PropertySource(value = "file:${user.home}/file/path/application.properties", ignoreResourceNotFound = false) })
public class PropConfig implements InitializingBean {

Now, I want to achieve the same using active profile. If dev profile is active, I need to fetch application-dev.properties, if stage profile is active, I want to fetch application-stage.properties and so on.

I am using Windows platform and JAVA 8 with Spring Boot 1.5.x

I tried setting the active profile in application.properties file. But it doesn't work

spring.profiles.active=dev
like image 371
Avinash Gupta Avatar asked Jul 04 '19 13:07

Avinash Gupta


1 Answers

Solution for Spring Boot 1.5.X

You can add the folder as a custom config location by running your app with the following JVM argument:

-Dspring.config.location=file:${user.home}/file/path/

With this JVM argument configured, all application-{profile}.properties files within this folder will be automatically resolved.

( Alternatively, if you prefer to use environment variables instead of JVM arguments, you can do the same thing by setting the SPRING_CONFIG_LOCATION environment variable, for example by using following command in the linux terminal: export SPRING_CONFIG_LOCATION=file:${user.home}/file/path/ )

Now, if you have a file application-dev.properties in your custom config folder, it should be enough to activate the profile in your default application.properties file by adding:

spring.profiles.active=dev

Finally, the @PropertySources annotation is redundant and you can remove it:

@Component
public class PropConfig implements InitializingBean {

Reference: https://docs.spring.io/spring-boot/docs/1.5.0.RELEASE/reference/html/boot-features-external-config.html


Solution for Spring Boot 2.X

The approach is mainly the same as for Spring Boot 1.5.X but with a slight difference.

In Spring Boot 2.X the behavior of the spring.config.location argument is slightly different than in earlier versions. The difference is that in Spring Boot 2.X the spring.config.location argument overrides the default config locations:

When custom config locations are configured by using spring.config.location, they replace the default locations. (Source: Spring Boot Documentation)

Since setting this argument to your custom config folder would override the default locations (I suppose that losing the config files on the default config locations is not the desired behavior), it is better to use the new spring.config.additional-location argument which doesn't override but only extend the default locations:

-Dspring.config.additional-location=file:${user.home}/file/path/

( Alternatively, you can use the SPRING_CONFIG_ADDITIONAL-LOCATION environment variable if you prefer to use environment variables instead of JVM arguments )

Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

like image 199
Marko Previsic Avatar answered Sep 23 '22 15:09

Marko Previsic