Search code examples
javaeclipsespring-bootapplication.properties

How to use Eclipse Variables in Spring Boot application.properties


I'm very new to Spring boot and I'm setting up my DB connection in then application.properties. Since I'd like to upload my project on GitHub I'm trying to access the datasource id and password from enviroment variables, instead of hardcoding them inside the properties file. But I'm not really sure about how to do it.

I'm using a Sql server instance running in a container, and I've tried the following:

  1. In "run configuration", in the Enviroment Tab, I added 2 enviroment variables : DB_USERNAME and DB_PASSWORD

  2. The application.properties looks like this:

spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)

But when i do run the spring boot application i get the following error:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '${DB_USERNAME)'

Can you help me figure out how to use the eclipse enviroment variables for this purpose in the right way?


Solution

  • Looks like there is a typo there:

    spring.datasource.username=${DB_USERNAME)
    spring.datasource.password=${DB_PASSWORD)
    

    Should actually be:

    spring.datasource.username=${DB_USERNAME}
    spring.datasource.password=${DB_PASSWORD}
    

    You can also specify default that would be applied if variables are not provided:

    spring.datasource.username=${DB_USERNAME:username}
    spring.datasource.password=${DB_PASSWORD:password}