Search code examples
spring-bootapplication.properties

Unable to populate system environment values in springboot application.properties


I am trying to access environment values in the application.properties file. The environment variables have been set in my system variables (Windows Environment Variables), say SPRING_DATASOURCE_USERNAME. But when I try to access that variable in my application.properties file by using:

spring.datasource.username=${SPRING_DATASOURCE_USERNAME}

That value is not getting populated. Rather I get a error:

2020-04-24 15:08:29.620 WARN 9892 --- [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : FATAL: password authentication failed for user "${SPRING_DATASOURCE_USERNAME}"

It is as if that ${...} is getting passed as a string completely. I am a beginner to this, so kindly point out the error if I am doing any. I am so far unable to find my mistake from the solutions to similar problems posted here.

NOTE: The database URL is hardcoded that's why I am getting password authentication error.


Solution

  • You don't need to do that at all!

    spring.datasource.username
    

    will be overwritten by the environment variable (User Variables) SPRING_DATASOURCE_USERNAME

    So you can remove this entry from the application.properties ( and restart your IDE in case the environment variables were added recently )

    Please read more about configuration in the Spring Boot docs: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config