Using dotenv files with Spring Boot

和自甴很熟 提交于 2020-12-26 07:53:13

问题


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.


回答1:


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:

  1. https://12factor.net/config
  2. Allow your code to be completely unaware of dotenv so that you continue to reference your application.yml/application.properties files using the normal Spring techniques. No funny business.



回答2:


There's actually a java port of 'dotenv' tool.

https://github.com/cdimascio/dotenv-java



来源:https://stackoverflow.com/questions/58549361/using-dotenv-files-with-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!