在Spring Boot中使用dotenv文件

问题描述:

我想使用dotenv文件配置我的Spring Boot应用程序.

I'd like to use dotenv files to configure my Spring Boot application.

做到这一点的最佳方法是什么?

What is the best way to do this?

Ruby 节点世界,我只是创建了 .env 文件,它将所有内容从那里加载到应用程序环境中.

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.

我已经在Spring和

I have built a proper integration between Spring and dotenv.

按照此线程理解动机.然后查看库:

Follow this thread to understand the motivation. And then review the library:

在此处查看spring-dotenv库:
https://github.com/paulschwarz/spring-dotenv

该库包含一个示例应用程序,向您展示如何使用它,并且在那里您看到与Spring的集成非常自然:

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. 允许您的代码完全不了解dotenv,以便您继续使用常规的Spring技术来引用application.yml/application.properties文件.没有可笑的事.