I'd like to use dotenv files to configure my Spring Boot application.
What is the best way to do this?
In Ruby or Node world, I just creating .env
file and it loads all stuff from there to application environment.
I don't like to create separate profiles for my app etc. I just want to load any environment variables I specified in file into my app.
I have built a proper integration between Spring and dotenv .
Follow this thread to understand the motivation. And then review the library:
Check out the spring-dotenv library here:
https://github.com/paulschwarz/spring-dotenv
The library includes a sample application to show you how to use it, and there you see that the integration with Spring is very natural:
https://github.com/paulschwarz/spring-dotenv/tree/master/application/src/main/resources
I stuck to two principles in designing this library:
There's actually a java port of 'dotenv' tool.
In spring boot just do that in application.yml
---
spring:
config:
import: optional:file:.env[.properties]
username: ${USERNAME}
or if you use application.properties
spring.config.import=optional:file:.env[.properties]
username=${USERNAME}
Then @value and all other stuff will work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.