Search code examples
javaspring-bootyamldevelopment-environment

How to concat strings in application.yaml?


I want to add two lines in a yaml file, or rather, I want the instance-id field to contain the result of adding the values of the port and name fields I tried using +, tried through various tags that I saw in similar questions, like !join, !concat (java says they don't exist). But nothing worked in the end. Is there a solution?

Example yaml (Sorry, i dont know how to load yaml here):

server:
  port: &port 62000
spring:
  application:
    name: &appname 'appname'
eureka:
  client:
    service-url:
      defaultZone: 'http://localhost:8081/eureka/'
  instance:
    instance-id: here should be *appname + ":" + *port (result appname:62000)


Solution

  • This won't work for YAML in general, but it will work in Spring Boot, since you can use Property Placeholders, and thus reference other properties from within a property value:

    server:
      port: 62000
    spring:
      application:
        name: 'appname'
    eureka:
      client:
        service-url:
          defaultZone: 'http://localhost:8081/eureka/'
      instance:
        instance-id: ${spring.application.name}:${server.port}
    

    This also works with environment variables, btw.