Search code examples
javaspring-boottomcat9

How to make sure that environment variable placeholders are substituted in a Spring Boot application running in Apache Tomcat?


I have a Spring Boot application, which runs in an Apache Tomcat server. In application.yaml I have, among others, following entries:

mail:
  pop3Host: ${MAIL_HOSTNAME}
  inboxFolder: ${MAIL_INBOX}
  hostName: ${MAIL_HOSTNAME}
  port: ${MAIL_PORT}
  userName: ${MAIL_USERNAME}
  password: ${MAIL_PASSWORD}

The application is deployed to Tomcat from within IntelliJ Idea so I can debug it.

I start Tomcat using the following command:

export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
./catalina.sh jpda start

However, after I

  1. start Tomcat using the above script,
  2. deploy the Spring Boot application from IntelliJ Idea, and
  3. make sure that the code where those values are used is executed,

I get the exception indicating that the placeholders have not been substituted.

How can I fix it, i. e. make sure that I can specify some information (like user name and password) in application.yaml via environment variables (so that I don't include the actual credentials in application.yaml)?


Solution

  • export JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8090,server=y,suspend=n"
    export JAVA_OPTS=" -DMAIL_HOSTNAME='smtp.provider.com' -DMAIL_INBOX='MAIL_INBOX' -DMAIL_PORT='587' -DMAIL_USERNAME='username' -DMAIL_PASSWORD='XXXXXXXX'"
    export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=n"
    ./catalina.sh jpda start
    

    Add export MAIL_HOSTNAME= etc. to the above lines, or create a setenv.sh file with such lines (in the same directory as catalina.sh file).

    Using setenv.sh is documented in RUNNING.txt file of Apache Tomcat.