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?

5
  • 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? Commented Dec 13, 2017 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. Commented Dec 13, 2017 at 23:28
  • Ok, so if you open another terminal and echo those variables without manually exporting them, what get's shown? Commented Dec 13, 2017 at 23:30
  • The user name is getting shown in a new terminal instance Commented Dec 13, 2017 at 23:54
  • Possible duplicate of spring - read environment variables from inside the application.properties file Commented Dec 14, 2017 at 4:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain how you did this?
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"
Make sure to restart tomcat :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.