0

I'm using Mac OS Mojave with Spring STS version 3.9.7.

So I'm trying to load some environment variables such as database username & password into my application.properties file but Spring fails to load them into.

I'v used this command to set the username environment variable:

export ABC_DB_UNAME=some_username

and when I do echo $ABC_DB_UNAME or printenv I can see the value.

In my application.properties file I set the username as below:

spring.datasource.username=${ABC_DB_UNAME}

Now when running my spring sts from the IDE itself, it is not able to pick up the values and it throws an exception that

access is denied for ABC_DB_UNAME@database_endpoint.

It clearly shows that spring sts is not picking up my environment variables.

What makes it more twisting for me is that when I run the spring app via maven using the below command, it picks up the environment variables and it works just fine.

mvn spring-boot:run

but using the mvn command I don't know how I can make the IDE to trigger the breakpoints for me to debug as well.

EDIT: Testing and trying to print the environment variable using the code System.getenv("ABC_DB_UNAME") and System.getProperty("ABC_DB_UNAME") also returns null but maven still works.

arash moeen
  • 4,533
  • 9
  • 40
  • 85
  • Not sure why this is marked as duplicate, I specifically stated that how I'm loading the env variables in my application.properties and the link proposed as the answer is by code approach. – arash moeen Jun 27 '19 at 01:48

1 Answers1

1

I think the environmental variable is set temporarily in the terminal. Hence the mvn spring-boot:run works. You can run it in different terminal, it should fail

You need to set the ${ABC_DB_UNAME} in the bashrc (mac) to persist it permanently. or you can pass the environmental variable as argument during running the app.

kars89
  • 48
  • 7
  • You are right, even though I managed to find the reason based on another stackoverflow topic that mentioned if the export is done manually in the shell, it'll be for that shell instance only. I moved everything into .bash_profile and restarted... it worked just fine. Accepting this answer still. – arash moeen Jun 27 '19 at 17:26