How can I externalize some config variables in spring boot? - java

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?

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.

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

How to use environment variables in flyway config file?

I have Flyway config file - flyway.properties , which contains basic database connection parameters:
flyway.url=jdbc:mysql://localhost
flyway.user=test
flyway.password=test
flyway.schemas=testdb
As I know exposing parameters in such config files is a bad practice. Is it possible to use environment parameters(to create .env file to define params there and to receive them in flyway config?)
You should be able to pass to the config file every possible property, including connections and passwords, so you don't have to store them in the config file. Something like this:
flyway -enterprise -url=jdbc:postgresql://localhost:5498/hamshackradio -user=postgres -password=dude1988 -configFiles="./conf/flyway.conf" migrate
That way you can use the environment variables from the command line much easier. This is the complete list of parameters. If you're calling the API, you can also use envVars(), but I don't have experience with that.
As well as using environment variables, you can use the flyway.conf file in the user profile folder, which only the current user can access. This is fine if you only access one database/server combination.
If you use several, then you can reference them as an extra .conf file in the user profile/user home area. For example, if, as a Windows user, you were accessing the development branch of a Pubs database project you might create a PubsDevelop.conf file in your user profile folder and specify it to Flyway by using a commandline parameter
flyway -configFiles="%userprofile%\PubsDevelop.conf" info
or if you dislike typing commands, set it up as an environemt variable
FLYWAY_CONFIG_FILES=%userprofile%\PubsDevelop.conf
That way, nobody can see your credentials or server name, and there is no risk of it leaking into the source control system by mistake!

How to append multiple values under the same property key?

I have multiple projects that expose a Prometheus endpoint, like so:
Application1: management.endpoints.web.exposure.include=info,health,prometheus
Application2: management.endpoints.web.exposure.include=info,metrics,prometheus
Application3: management.endpoints.web.exposure.include=info,refresh,prometheus
Instead of adding this Prometheus config to each application separately, I have extracted this to a separate library and added it as a dependency. The lib's application.properties file only consists management.endpoints.web.exposure.include=prometheus.
Whenvever I start up my applications, all of their config is overwritten with prometheus, instead of the value being added to the back of the list.
Is it possible to append multiple values to the same key instead of it being overwritten?
Sounds like you need to use profiles feature. Profiles means that you can run the same project with different profiles where each profile can have its own property files and even different bean implementations per profile. Read this article about profiles and see if that will help: Spring Profiles

How can I make a profile-specific bootstrap.yml?

I want to have different property values in a bootstrap.yml file depending on a spring profile passed to a starting application (the same way as it works for application-{profile-name}.yml).
How is it possible?
According to the documentation it should work pretty much the same.
Changing the Location of Bootstrap Properties:
If there is an active profile (from spring.profiles.active or through the Environment API in the context you are building), properties in that profile get loaded as well, the same as in a regular Spring Boot app — for example, from bootstrap-development.properties for a development profile.
That is it should be possible to suffix the bootstrap yaml with the environment name to make it take effect.

Load Values from configuration File using spring

I am looking for a clean way to load values from properties file using the environment value passed from the command line (maven project). I then plan to use the loaded values in Java cucumber test.
Usage : mvn test -Denvironment=staging (it then loads the values from the staging file i.e. application-staging.properties, etc).
I know spring boot offers profiles, wondering if I can do this use a clean approach using the generic spring framework (annotations).
Thanks #racraman. I am able to get this to work using your suggestion around PropertySource. In order to get different environment working i've used something like :
#PropertySource({"classpath:${env}.properties"}).
Cheers !!!

Best way to reference the file system in a spring mvc app

In a spring mvc application, what is the best way to reference the filesystem?
Say I want to know the root of my applications path?
Should I create a properties file and hard code this value in the property file, then create different versions for production and development environments?
I might want to reference a file outside of my application also, so I guess a property file is best suited for this correct?
I understood your question as a config/release problem, not coding problem. If you want to access file (say with absolute path) there are different ways to achieve it:
if you use maven to build your app. create maven profile with corresponding property, e.g. file.path and at build-time fill the property to spring bean (e.g. a String)
create different properties files, which containing config parameters for different environments. and let maven fill the placeholder in spring conf, which properties file should be used.
use spring profile. put server-relevant beans in profiles, and your application choose the right profile (the set of beans) at runtime.
well if you have different databases for different environments, you could consider to save some config parameters in a config table. And application loads those data when it starts or when it needs. At least this is another option.
did that answer your question? or I am just talking about something else?...
Get real path and complete your remaining action
String realContextPath = session.getServletContext().context.getRealPath(request.getContextPath());

Categories

Resources