21

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?

JayC
  • 2,144
  • 8
  • 42
  • 77
  • 1
    See: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html and http://stackoverflow.com/questions/28303758/how-to-use-yamlpropertiesfactorybean-to-load-yaml-files-using-spring-framework-4 – Dave Jarvis Mar 31 '17 at 20:50
  • Thank you @DaveJarvis But I don't think that is what I am looking of. – JayC Apr 03 '17 at 15:24
  • Have you tried searching for "username" inside of the YAML or other configuration files used by the application? It will likely be difficult for anyone to answer this question without knowing the contents of the files or the folder structure for the application. You should be able to search through the files, though, to find where the variables are defined. – Dave Jarvis Apr 03 '17 at 18:50
  • @DaveJarvis That is what I am trying to understand myself. I know it's a bootstrap.yml file. I just don't understand where the placeholder is receiving their values from. I followed your method and searched up "username". I see that build.gradle has "username = "${artifactory_user}". But "artifactory_user" does not lead to any where else. – JayC Apr 03 '17 at 21:14
  • https://github.com/JetBrains/spek/issues/107 – Dave Jarvis Apr 04 '17 at 00:11
  • **See also:** https://stackoverflow.com/questions/41620674/use-placeholders-in-yaml – dreftymac Sep 30 '19 at 23:24

5 Answers5

29

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.

JayC
  • 2,144
  • 8
  • 42
  • 77
  • 1
    @IslamAzab When you are running this on the local. 1) press the windows key 2) type "environment variables" 3) press edit environment variables (this is where you set key/values) – JayC Mar 12 '18 at 19:12
  • Not only with Env, check this other alternative: https://stackoverflow.com/questions/54883144/how-to-resolve-placeholder-in-properties-file-with-values-from-another-propertie?answertab=active#tab-top – Ualter Jr. Jun 24 '19 at 10:43
1

After doing some research and experiments I found out placeholders can be both environment variables and command line arguments. The syntax for properties file worked in YAML file too. Environtment variable has already been explained by @Jesse. If you lets say pass command line argument:

--my.stored.files.username=UsernameSample or --username=UsernameSample, the configuration attribute would be filled as expected

my:
 stored:
  files:
   username: ${username:defaultUsername}

I hope this might be helpful to someone having a similar issue.

ddsultan
  • 2,027
  • 1
  • 19
  • 19
0

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

  • A /config subdirectory of the current directory
  • The current directory
  • A classpath /config package
  • The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

You can also use YAML ('.yml') files as an alternative to '.properties'.

If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:

 $ java -jar myproject.jar --spring.config.name=myproject

The following example shows how to specify two locations:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).

If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.

Config locations are searched in reverse order. By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. The resulting search order is the following:

  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

When custom config locations are configured by using spring.config.location, they replace the default locations. For example, if spring.config.location is configured with the value classpath:/custom-config/,file:./custom-config/, the search order becomes the following:

  • file:./custom-config/
  • classpath:custom-config/

Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:

  • file:./custom-config/
  • classpath:custom-config/
  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
0

ddsultan's answer works for me. just in case someone like me need to set placeholder value in idea. Edit run configuration, click Modify options, then check "Program options", in the newly added field, input the place holder value, for example "--section=4".

enter image description here

SalutonMondo
  • 629
  • 9
  • 17
-1

Please use {{your key}} as a place holder in case of .yml file

user1608528
  • 1
  • 1
  • 5