paulschwarz / spring-dotenv

Provides a Dotenv property source for Spring
MIT License
299 stars 23 forks source link

Spring Boot unable to read values for datasource #30

Open x80486 opened 4 months ago

x80486 commented 4 months ago

Description

I have a simple Spring Boot (3.2.2) application. I want to start using spring-dotenv for local development, so I started configuring the datasource to use environment variables instead, but somehow the application is not able to pick up the values from the .env file.

developmentOnly("me.paulschwarz:spring-dotenv:4.0.0")

I imported spring-dotenv in Gradle using developmentOnly but I've also tried implementation.

# application.yaml
spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    type: oracle.ucp.jdbc.PoolDataSource
#
# File must be at the project's root

SPRING_DATASOURCE_PASSWORD = "bar"
SPRING_DATASOURCE_URL = "jdbc:oracle:thin:@tcp://database-hostname:1521/service-name"
SPRING_DATASOURCE_USERNAME = "foo"

Environment

$ ./gradlew --version 

------------------------------------------------------------
Gradle 8.6
------------------------------------------------------------

Build time:   2024-02-02 16:47:16 UTC
Revision:     d55c486870a0dc6f6278f53d21381396d0741c6e

Kotlin:       1.9.20
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          21.0.2 (Eclipse Adoptium 21.0.2+13-LTS)
OS:           Linux 6.6.11-2-lts amd64
x80486 commented 4 months ago

I just tried declaring the environment variables within the operating system (read: export VAR="value") and the application worked fine — so I'm certain it's an issue with the library.

Somehow, when I start the application with Gradle using ./gradlew bootRun it doesn't read them from .env.

Moreover, the problem is with the built-in environment variables that Spring Boot recognizes. I just remapped the variables and it works fine:

spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    password: ${DATASOURCE_PASSWORD:?error}
    type: oracle.ucp.jdbc.PoolDataSource
    url ${DATASOURCE_URL:?error}
    username: ${DATASOURCE_USERNAME:?error}

...but looks like anything starting with SPRING_ doesn't currently work.

miroslavvojtus commented 4 months ago

I am facing the same issue. I believe the library somehow circumvents the Springs env to property resolution. Most probably introduces the environment variable too late in the resolution chain.

marchrius commented 1 week ago

Hi, same issue here. The problem is not about SPRING_ prefixed environment variable but about the relaxed binding as described in #16