Search code examples
javaspring-bootyamlmanifeststring-interpolation

How to use Property placeholders in .yml file


I am working with Java and spring boot. I was wondering how to add Property placeholders into .yml files. I've found some crisp examples but I am not sure where are the Property placeholders are being instantiated in. Is it in system env variables, a file, etc..?

Bootstrap.yml

spring:
  cloud:
    config:
      username: ${my.stored.files.username}
      password: ${my.stored.files.password}
      label: ${spring.cloud.find.label}
      uri: ${spring.cloud.config.uri}
      enabled: false
      failFast: true

User is using Property placeholders, but where did the user declared them? Where is this .yml reading the values from? (same question as above) Is there a document that explains the connection?

This web application will be pushed to cloud foundry using "cf push", Which will automatically pick manifest.yml file to configure. If possible a cloud foundry example would be great.

Understanding/ Sample Application.properties file

app.name=MyApp
app.description=${app.name} 

User was able to use ${app.name} because it is defined. I am confused on the example above. How and where is the user getting "${my.stored.files.username}. Where is that being defined? I assumed it would be in system.properties or environment variables. Can anyone confirm?


Solution

  • After intensive research, I was able to find that when I use placeholders in .yml files it reads that values from environment variables. Which was part of my theory in the beginning, but no one has confirmed.

    Answer for local environment

    spring:
      cloud:
        config:
          username: ${my.stored.files.username}
          password: ${my.stored.files.password}
          label: ${spring.cloud.find.label}
          uri: ${spring.cloud.config.uri}
          enabled: false
          failFast: true
    

    *In environment variables *

    Image as example

    set key as: my.stored.files.username
    set value as: UsernameSample
    

    Then

    When you run application, yml will read like so.

        config:
          username: ${my.stored.files.username}
                    //gets replaced with UsernameSample
    

    This is the link that solved my problem link

    For Cloudfoundry

    You would have to create cups or manually add these variables onto the service.