Home > Net >  How can I externalize some config variables in spring boot?
How can I externalize some config variables in spring boot?

Time:04-26

I have a yaml file that contains some credentials for azure keyvault. This project is I am working on is a shared git repo so I would like to set these values as environment variables for the whole project not just for myself

application.yml:

azure:
   keyvault:
      uri:someUri
      client-id:someClientId
      client-key:someClientKey

but I want to have them set up like this:

azure:
   keyvault:
      uri: ${uri}
      client-id:${clientId}
      client-key:${clientKey}

Is there a way to set those values and have this work for others without them having to manually set these values in their environment?

CodePudding user response:

You can ignore the .yaml in gitignore so you can use your config in your environment. And if you pull from others their config won't overwrite your config.

If you want if like dynamically

java -jar myapp.jar --spring.application.json='{"foo":"bar"}'

You can add variables like this in start command. Configure in your Run configuration.

CodePudding user response:

The syntax you've presented above is supposed to work. I usually use capital letters for 'convention' but other than that its fine. However providing such a syntax effectively means that you don't want to define property values in your application so you'll have to get them from elsewhere.

From your question I understand is that your primary concern is your teammates and in your setup each one of your peers has clientId, key, etc.

In this case you can create a script that will "export" all the variables to be environment variables automatically, they'll run it only one time and it will work since than.

Another option which is kind of similar is providing a property SPRING_APPLICATION_JSON that will refer to the json with a configuration, spring boot application can read it as written in the official documentation

  • Related