Spring-Dotenv integrates the excellent
dotenv-java library into the Spring
ecosystem by exposing .env files as a Spring
PropertySource.
It lets you:
- Load environment variables from a local
.envfile during development - Keep production behavior unchanged (real environment variables always win because
.envvalues are added after system environment variables, preserving standard Spring precedence) - Follow The Twelve-Factor App principle of configuration via environment
The .env file is strictly a development convenience. It should never be
committed to source control.
📄 See the CHANGELOG.md for breaking changes, upgrades, and release notes.
- Java 17 or newer
- Spring Framework or Spring Boot 3 / 4
If you need Java 8 or 11 support, use an older release.
This project is published as multiple artifacts. Choose exactly one at runtime.
-
spring-dotenv
Core library for Spring Framework (no Spring Boot auto-integration) -
springboot3-dotenv
Auto-integrates with Spring Boot 3 -
springboot4-dotenv
Auto-integrates with Spring Boot 4
A BOM is provided to keep versions aligned across modules.
dependencies {
implementation(platform("me.paulschwarz:spring-dotenv-bom:$version"))
developmentOnly("me.paulschwarz:springboot4-dotenv")
}<dependencyManagement>
<dependencies>
<dependency>
<groupId>me.paulschwarz</groupId>
<artifactId>spring-dotenv-bom</artifactId>
<version>$version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>me.paulschwarz</groupId>
<artifactId>springboot4-dotenv</artifactId>
<optional>true</optional>
</dependency>
</dependencies>Add .env to your .gitignore:
.envIt’s common to commit a .env.example file instead, documenting the available
variables without leaking secrets.
Assume a simple Spring application that needs a configurable value:
@Value("${example.name}")
String name;Declare the property in application.yml:
example:
name: ${EXAMPLE_NAME:World}Create a local .env file:
EXAMPLE_NAME=PaulAt runtime:
- If
EXAMPLE_NAMEis set in the real environment, that value wins - Otherwise, the value from
.envis used - If neither is present, the default (
World) applies
This preserves production safety while making local development frictionless.
Keys loaded from .env participate in relaxed name resolution when running under Spring Boot (3 or 4).
In plain Spring Framework applications, .env keys are resolved strictly.
For example, under Spring Boot:
MY_SERVICE_URL=https://example.comcan be accessed as:
my.service.urlAll configuration is optional. Defaults are sensible.
-
Spring Boot 3 / 4
- Relaxed binding is applied to:
springdotenv.*configuration keys- variables defined inside
.envfiles
- Standard Spring Boot rules apply (kebab-case, camelCase, uppercase environment variables).
- Relaxed binding is applied to:
-
Plain Spring Framework
- Configuration is strict:
springdotenv.*keys must match exactly.enventries must match exactly
- No relaxed binding is applied.
- Configuration is strict:
| System property | Environment variable | Default value |
|---|---|---|
springdotenv.enabled |
SPRINGDOTENV_ENABLED |
true |
springdotenv.directory |
SPRINGDOTENV_DIRECTORY |
|
springdotenv.filename |
SPRINGDOTENV_FILENAME |
.env |
springdotenv.ignoreIfMalformed |
SPRINGDOTENV_IGNORE_IF_MALFORMED |
false |
springdotenv.ignoreIfMissing |
SPRINGDOTENV_IGNORE_IF_MISSING |
true |
springdotenv.exportToSystemProperties |
SPRINGDOTENV_EXPORT_TO_SYSTEM_PROPERTIES |
false |
The keys shown above support relaxed binding only in Spring Boot applications.
When using the core spring-dotenv module without Spring Boot, these keys must be specified exactly.
-
springdotenv.enabled
Defaults totrue. Set tofalseto completely disable integration. -
springdotenv.exportToSystemProperties
If enabled, variables loaded from.envare also exported toSystem.getProperties().
This field was previously known assystemPropertiesbut was renamed for clarity. It is deprecated but still supported with warning.
If both the legacy and the canonical property are set, the canonical key (springdotenv.exportToSystemProperties) takes precedence.
WhenexportToSystemPropertiesis enabled,.envvariables are exported verbatim. Relaxed name resolution is applied only when values are read via Spring’s Environment. -
springdotenv.ignoreIfMissing
Defaults totrue. Set tofalseto fail fast when.envis absent.
Older versions required properties to be prefixed (for example env.EXAMPLE_NAME).
This is no longer the case. Prefixes have been removed.
Spring-Dotenv follows the configuration rules of the runtime it integrates with.
When used with Spring Boot 3 or 4, all springdotenv.* configuration options and .env file entries participate in standard Spring Boot relaxed binding.
This means configuration keys are interchangeable across common naming styles:
springdotenv.ignore-if-missingspringdotenv.ignoreIfMissingSPRINGDOTENV_IGNORE_IF_MISSING
Likewise, variables defined in .env files are resolved using the same relaxed rules when accessed through the Spring Environment. For example:
MY_SERVICE_URL=https://example.comcan be consumed as:
my.service.urlThis behavior is delegated to Spring Boot’s native Binder and matches Boot’s built-in configuration semantics.
When using the core spring-dotenv module without Spring Boot, all configuration is strict:
springdotenv.*keys must match exactly.enventries are resolved by exact name only- No relaxed binding or env-var style name conversion is applied
This distinction is intentional and mirrors the behavior of Spring Framework itself.
./gradlew buildPull requests are welcome.
For substantial changes, please open an issue first to discuss scope and approach. Tests are expected for behavioral changes.
MIT © Paul Schwarz