Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read value from .env file in Spring Boot

In my Spring Boot app, I have the following values in my .env file located in the project root:

.env:

DB_NAME=demo_db
DB_USERNAME=postgres
DB_PASSWORD=pass

On the other hand, when I set environment variebles on the Run configuration of IntelliJ as shown below, it is working:

DB_NAME=demo_db;DB_USERNAME=postgres;DB_PASSWORD=pass

So, I have the following questions:

1. As far as I know, .env file is used to keep environment variables instead of setting. Is that true? Could you explain the usage of it a little bit more (I look at several pages but not a brief explanation).

2. Is there any relation of .env file with application.properties except from reading variable values from .env file? I am not sure if there may also be different version o .env file e.g. .env-dev file ?


1 Answers

Apparently you are mixing up spring boot and InteliJ.

  • .env will be used by IntelliJ if you have EnvFile - IntelliJ IDE Plugin (don't forget to enable it in run config and of 2024-10 it was also necessary to set "Enable experimental integrations")
  • application.properties looked up by spring boot for injecting spring properties

If you are looking to run the application detacted from IDE you should define them in application.propeties defining them in a profile specific approach. Consider this article from offical spring docs.

like image 173
krishnan Avatar answered Jul 26 '26 13:07

krishnan