spring boot usage of placeholder in application properties

11/13/2018

I have written a simple spring boot application(version springboot 2.0) which uses mysql(version 5.7).

application.properties snippet

spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = testuser
spring.datasource.password = testpassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

When I run it locally, it works fine. If I want to run this spring boot application in docker then I can change

spring.datasource.url = jdbc:mysql://mysql-container:3306/test?useSSL=false

mysql-container is run using mysql:5.7 image from dockerhub.

However I want to change value of host from some placeholder properties file. so that this looks something like:

spring.datasource.url = jdbc:mysql://${MYSQL_HOST}:3306/test?useSSL=false

note: I am not sure about placeholder format. Is it ${MYSQL_HOST} or @MYSQL_HOST@ ?

you can name this placeholder file as placeholder.properties or placeholder.conf or .env or anything. The content of that file should be something like:

MYSQL_HOST=localhost

or

MYSQL_HOST=some ip address

I can create .env or .env.test or .env.prod and I can refer that env file based on where I want to run application.


UPDATE -

I have two questions:

  1. Where should I keep placeholder.properties? Is it under /config/ or under some specific directory?

  2. how to invoke placeholder inside application.properties ?

can someone suggest?

-- Shivraj
docker
kubernetes
mysql
spring-boot

3 Answers

1/18/2019

If you project is Maven you can use maven filter:

<build>
  <filters>
            <filter>src/main/filters/myfilter.properties</filter>
  </filters>
</build>

This generates the /target/classes/application.properties it has been filtered to contain the property values (with replaced placeholders)

http://www.avajava.com/tutorials/lessons/how-do-i-filter-resources-based-on-values-from-a-properties-file.html?page=1

-- sytolk
Source: StackOverflow

11/14/2018

Thanks to answers by @Raheela Aslam and @paulsm4 and some more research found the issue.

What I was trying to achieve:

  1. Deploy springboot application in docker and then to kubernetes.
  2. I was using minikube for local testing and wanted to pass minikube ip to datasource url.

How I fixed it:

I created configmap for mysql_user, mysql_password, mysql_host with respective values.

kubectl create configmap mysql-config \
--from-literal=mysql_user=testuser \
--from-literal=mysql_password=testuserpass \
--from-literal=mysql_user=$(minikube ip) 

and used these inside application.properties something like below

spring.datasource.url = jdbc:mysql://${MYSQL_HOST}:3306/test?useSSL=false
spring.datasource.username = ${MYSQL_USER}
spring.datasource.password = ${MYSQL_PASSWORD}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

and then used configmap values in deployment.yaml for kubernetes. Then did start service for deployment.

-- Shivraj
Source: StackOverflow

11/13/2018

SUGGESTION: If you have a relatively small #/properties, why not just have a different application.properties file for each different environment?

You'd specify the environment at runtime with -Dspring.profiles.active=myenv.

Look here and here.

PS:

To answer your specific question: the syntax is ${MYSQL_HOST}

-- paulsm4
Source: StackOverflow