5

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
  • 1,820
  • 16
  • 30
Avinash Gupta
  • 208
  • 5
  • 18
  • Can you influence how the application is started? You could put the directory on the classpath so that the default Spring Boot mechanism works? – Wim Deblauwe Jul 04 '19 at 14:42
  • I am running the application using Eclipse. I am running the PSVMain() method of the application. The same works when propertis files are in class path i.e in resources folder. I am not able to configure it outside classpath – Avinash Gupta Jul 04 '19 at 16:01

2 Answers2

9

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

Marko Previsic
  • 1,820
  • 16
  • 30
  • This definitely works for a single project running on a machine. What if I have multiple Spring boot projects? – Avinash Gupta Jul 05 '19 at 10:29
  • I don't see what the difference is when running multiple projects. In the case of running multiple projects, you will have to apply these settings to each of the projects, let me know if I missed something? – Marko Previsic Jul 05 '19 at 10:58
  • 1
    I followed the Environment variable way. Now the scenario is, I have one project for which I have configured the Environment variable **SPRING_CONFIG_LOCATION**. Now I have another project for which properties files are at different location. How can I configured path for that? – Avinash Gupta Jul 05 '19 at 11:04
  • 2
    In this case, I would prefer the other way - to use the JVM argument instead of the environment variable. If you can only use an environment variable, I think that the only way to have a separate value for each project would be to run each of the apps with a different user and then set a different value for the environment variable for each user ( you can find here how to do it: https://serverfault.com/questions/583185/creating-an-environment-variable-for-a-specific-user ). But I don't know if having several users for your apps would be an option. Can you try it with the JVM arg approach? – Marko Previsic Jul 05 '19 at 11:11
0

Can you please try setting the active profile using a JVM argument as below- -Dspring.profiles.active=dev

If your requirement is to restrict a particular bean to be used in an environment, then the @Profile("dev") annotation can be used.

This reference may help you -> https://www.baeldung.com/spring-profiles

Leo Varghese
  • 165
  • 1
  • 2
  • 12
  • Thanks for your response. I tried above. Didn't worked. My application.properties files are outside the project directory. What I want is to set the active profile in application.properties file and based on the profile, need to pick application-{profile}.properties file – Avinash Gupta Jul 04 '19 at 13:33