Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot .env variables in application.properties

I have created an .env file where I keep variables. I want them to be defined in my application.properties, but this is not working. What do I need to add to get the variables?

.env file

MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=root

application.properties

spring.profiles.active = dev

application-dev.properties

# mysql database properties
spring.datasource.url = jdbc:mysql://localhost:3306/testdb?useUnicode=true&serverTimezone=UTC&server&createDatabaseIfNotExist=true
spring.datasource.username = ${MYSQLDB_USER}
spring.datasource.password = ${MYSQLDB_ROOT_PASSWORD}
        
# hibernate properties
spring.jpa.hibernate.ddl-auto = create
spring.jpa.show-sql = true
like image 416
Dratewka Avatar asked Mar 19 '26 13:03

Dratewka


1 Answers

This won't happen automatically. You can make it work by adding this to your application-dev.properties:

spring.config.import=optional:file:.env[.properties]

Another option is to use an additional library that adds support for this, such as https://github.com/paulschwarz/spring-dotenv.

There is some discussion about adding support for this in Spring Boot, but it is not a high priority.

like image 126
Scott Frederick Avatar answered Mar 22 '26 05:03

Scott Frederick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!