Ashish Goyal
Ashish Goyal

Reputation: 67

How to access environment variable externally in spring boot?

Is it possible to access environment variable of different application.properties from a single place. Actually we are building this software where we have different application.properties for different projects like user-asset. So is it possible to have all environment variable at one external place. If yes, how will it be accessed?

Upvotes: 1

Views: 852

Answers (2)

Kyle Pfromer
Kyle Pfromer

Reputation: 1575

You would have an application.properties file that defines variables that would never change in what every situation you have.

application.properties

server.error.whitelabel.enabled=true #Just an example

Then you could have a separate application.properties with a different name such as application-active.properties. This file would add onto the base application.properties file.

application-active.properties

example.enviroment.variable=${I_AM_AN_ENVIROMENT_VARIABLE}

Then you could have a different application.properties file that has the same property name, in this case example.enviroment.variable.

application-dev.properties

example.enviroment.variable=${I_AM_A_DIFFERENT_ENVIRONMENT_VARIABLE}

Then in your code, you would just need to grab the example.enviroment.variable property depending on the current profile and it would grab the correct environment variable.

To specify what application.properites look at using profiles in spring-boot.

Upvotes: 1

Yasin
Yasin

Reputation: 2039

If its an environment variable, then I think multiple applications can access the same variable.

But if its inside the application.properties file, then I think its not possible.

Not only that, if you really feel that one application needs to access the application.properties of another, then I believe this is not a right way to proceed.

Rather, you should externalize the configurations (maybe by using a config server like spring-cloud-config) and share the common properties between the applications.

Upvotes: 0

Related Questions