Does application.yml support environment variables?
Yes, Spring Boot's application.yml
can utilize environment variables. It's accomplished through a ${ENV_VAR}
sort of notation or placeholders. Here's an example:
In the example above, the variable ${PORT}
represents the server's port number. If PORT
is defined in your environment variables, its value would be used. Otherwise, it defaults to 8080
.
This is how you inject environment variables into your application.yml
. Furthermore, these values can be fetched using Spring's placeholder notation, like the ${OPENSHIFT_DIY_PORT}
example above.
To fully understand how these placeholders work, it is beneficial to immerse yourself in Spring Boot's documentation. It offers comprehensive guidance on how to use application.yml
to its full potential.
Your YAML friend — Environment variables
Providing life support with @Value
Spring's @Value
annotation acts as a lifebuoy for environment variables, injecting them directly where they're needed:
Can't find the variable HOME_SWEET_HOME
on your system? Not to worry, Spring's got your back — /root
is the default lifeguard on duty.
Case of the command line overrides
Need to inject dynamically changing variables (like the mood swings of a sleepy developer 😴)? Try command line arguments; they can override any aristocrat living peacefully in application.yml
:
System Defaults — Your fallback buddies
What's better than a friend, you ask? A default friend that sticks around when everyone else is missing. Define them in application.yml
like this ${VAR_NAME:default_friend}
:
In case DB_HOST
or DB_PORT
forget to show up, you'll find localhost
(or 3306
) waiting to connect you to your database.
IDE interventions
When you need a break from terminal-schminal commands, your trusty IDE (like IntelliJ) steps in. It offers handy features to set, get, and manage environment variables. It's like getting all the backstage-access passes to how environment variables are handled.
Vault of secrets
Know where to hide your secrets
To keep your secrets safe, avoid hardcoding sensitive settings (like passwords, API keys) in application.yml
or anywhere else in your codebase:
Using environment variables for sensitive data not only sanitizes your app configs but also makes the process of credential rotation hassle-free.
Dance of the file-specific variables
For more granular control over configurations, use profiles with individually dressed-up application-{profile}.yml
files:
Environment-specific files ensure you're always wearing the right outfit for the right environment, without tripping over inappropriate settings.
Crossing the environmental differences
Research before you leap
Be aware that the rules of the environment variable game can change with the operating platform or cloud provider. So, research the syntax conventions for the tech stack you're using.
Keep your configuration off the dancefloor
While it's fun to throw configurations around in your code, it's good practice to keep configuration separate from code. This contributes to better maintainability and flexibility.
Make it foolproof
When dealing with multiple environments and complex settings, make sure to test different methods of injecting and resolving environment variables. This ensures that no matter if you're developing locally or deploying to Jurassic Park, your app configurations will work flawlessly.
Was this article helpful?