I’m trying to load environment variables from a .env
file in my Spring Boot app using:
implementation 'io.github.cdimascio:java-dotenv:5.2.2'
Here’s part of my application.properties:
spring.datasource.url=${DB_URL} spring.datasource.username=postgres spring.datasource.password=${DB_PASSWORD} stripe.api.key=${STRIPE_API_KEY}
My .env file:
DB_URL=jdbc:postgresql://localhost:5432/mydb DB_PASSWORD=mysecretpassword STRIPE_API_KEY=my_stripe_key
The environment variables are not being loaded into application.properties
when I run the app, and I get errors like:
URL must start with 'jdbc'
I tried putting .env
file is in the root directory and the Dotenv
class reads the variables correctly in the main method.
How can I properly load the .env
variables into Spring Boot’s application.properties
using java-dotenv
?
I wanted the .env to work and connect but it wont.
0
You should try to look at https://github.com/paulschwarz/spring-dotenv spring-dotenv
project which injects .env
file as spring-property-source
See another question like yours : Using dotenv files with Spring Boot