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
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.