17

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.

Paul Schwarz
  • 1,828
  • 1
  • 15
  • 24
Vova Rozhkov
  • 1,582
  • 2
  • 19
  • 27

3 Answers3

23

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

Bruno Lee
  • 1,867
  • 16
  • 17
  • 1
    spring.config.import=file:/my/path/to/passwords.env[.properties] Here passwords.env does not have to be on the classpath and is required (no optional: modifier before file:) – Andrew Mar 08 '22 at 06:46
  • 1
    Where do you place your .env file with this solution? I'm new to all this, but I would guess you can put it anywhere and then you have to add that location to your classpath or something. Is there any way of doing this though that you people would recommend? Assuming you made your project using Spring Initializr maybe. – Kevin Wheeler Jun 17 '23 at 14:46
15

I have built a proper integration between Spring and dotenv.

Latest releases:

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.
Paul Schwarz
  • 1,828
  • 1
  • 15
  • 24
  • 1
    Today I published 2.5.3 which addresses the vulnerability found recently in Spring. Please update to the latest. https://github.com/paulschwarz/spring-dotenv/releases – Paul Schwarz Apr 21 '22 at 13:51
  • Latest releases: 3.0.0 for Java 8 4.0.0 for Java 11 https://github.com/paulschwarz/spring-dotenv/releases – Paul Schwarz May 11 '23 at 08:33
3

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

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

cmd
  • 11,622
  • 7
  • 51
  • 61
Maksym Govorischev
  • 764
  • 2
  • 5
  • 12
  • Well it doesn't does the same job as dotenv guys in ruby and node world. First, it can't inject env variables into the process, the reason is simple—JVM prohibits this (https://github.com/cdimascio/java-dotenv#faq) Second, It doesn't have integration with Spring Boot. I'll write workaround for this and will edit answer with the complete solution a bit later. And by the way, It doesn't support different .env files for different environments (development, production, etc). Java world is tough. – Vova Rozhkov Nov 01 '19 at 19:57
  • 1
    note that you can use a different .env file per environment by using java-dotenv's "filename" property. that being said, the wider dotenv community recommends using a single .env file per environment – cmd Mar 13 '20 at 23:49