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

Avinash Gupta :

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
Marko Previsic :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=121690&siteId=1