0

I have exported some simple db creds on my machine as ENV variables.

export MYSQL_USER=root
export MYSQL_PASS=pass

I'm trying to load these dynamically into my profile into spring boot, but to no avail. I'm starting to believe this is because the application runs inside a TomCat container and therefore isolated from the machine env vars.

Development Environment Application Properties File

spring.datasource.username = ${MYSQL_USER}

However, it's still being seen as literal plain text of ${MYSQL_USER}. Does Spring Boot have the capability for readingg in system environment variables?

Ryan S
  • 155
  • 2
  • 16
  • Could you provide a sample project? Are you sure those values are being loaded properly by the profile/user that your SpringBoot app is running under? For example, did you add those exports to a "/etc/profile.d/mysql.sh" file? – Pytry Dec 13 '17 at 23:13
  • I simply exported these in terminal as shown above, but I have no idea how to actually load system environment variables into Spring Boot. – Ryan S Dec 13 '17 at 23:28
  • Ok, so if you open another terminal and echo those variables without manually exporting them, what get's shown? – Pytry Dec 13 '17 at 23:30
  • The user name is getting shown in a new terminal instance – Ryan S Dec 13 '17 at 23:54
  • Possible duplicate of [spring - read environment variables from inside the application.properties file](https://stackoverflow.com/questions/39703453/spring-read-environment-variables-from-inside-the-application-properties-file) – buræquete Dec 14 '17 at 04:38

1 Answers1

1

I was also having same problem to get OS env variables into my application.properties/yml using the $ sign. To solve this, I defined the variables to application server(tomcat/jboss etc). Then only the application.properties/yml can get them. Hope this will help.

Myth
  • 86
  • 2
  • 10
  • Can you explain how you did this? – Ryan S Dec 14 '17 at 05:53
  • Many ways to do that depending on application server. As you are using tomcat then you can follow as below: Under Tomcat 's /bin/ create one file - setenv.sh-  (touch setenv.sh) then open (sudo nano setenv.sh) and write #!/bin/sh export JAVA_OPTS="-DMYSQL_USER=myuser" – Myth Dec 14 '17 at 06:20
  • Make sure to restart tomcat :) – Myth Dec 14 '17 at 06:23