Chapter 7. Property expressions

Property expressions are combinations of property references and plain text strings that you can use to substitute values of properties in your configuration.

Much like a variable, you can use a property expression in Quarkus to substitute a value of a configuration property instead of hardcoding it. A property expression is resolved when java.util.Properties reads the value of the property from a configuration source in your application.

This means that if a configuration property is read form your configuration at compile time, the property expression is also resolved at compile time. If the configuration property is overriden at runtime, its value is resolved at runtime.

Property expressions can be resolved using more than one configuration source. This means that you can use a value of a property that is defined in one configuration source to expand a property expression that you use in another configuration source.

If the value of a property in an expression cannot be resolved, and you do not set a default value for the expression, your application encounters a NoSuchElementException.

7.1. Example usage of property expressions

In this section you can find examples of how you can use property expressions to achieve greater flexibility when configuring of your Quarkus application.

  • Substituting the value of a configuration property:

    You can use a property expression to avoid hardcoding property values in you configuration. Use the ${<property_name>} syntax to write an expression that references a configuration property, as shown in the following example:

    application.properties

    remote.host=quarkus.io
    callable.url=https://${remote.host}/

    The value of the callable.url property resolves to https://quarkus.io/.

  • Setting a property value that is specific to a particular configuration profile:

    In the following example, the %dev configuration profile and the default configuration profile are set to use data source connection URLs with different host addresses. Depending on the configuration profile with which you start your application, your data source driver uses the database URL that you set for the profile:

    application.properties

    %dev.quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false
    quarkus.datasource.jdbc.url=jdbc:mysql://remotehost:3306/mydatabase?useSSL=false

    You can achieve the same result in a simplified way by setting a different value of the custom application.server property for each configuration profile. You can then reference the property in the database connection URL of your application, as shown in the example:

    application.properties

    %dev.application.server=localhost
    application.server=remotehost
    
    quarkus.datasource.jdbc.url=jdbc:mysql://${application.server}:3306/mydatabase?useSSL=false

    The application.server property resolves to the appropriate value depending on the profile that you choose when you run your application.

  • Setting a default value of a property expression:

    You can define a default value for a property expression. Quarkus uses the default value if the value of the property that is required to expand the expression is not resolved from any of your configuration sources. You can set a default value of an expression using the following syntax:

    ${<expression>:<default_value>}

    In the following example, the property expression in the data source URL uses mysql.db.server as the default value of the application.server property:

    application.properties

    quarkus.datasource.jdbc.url=jdbc:mysql://${application.server:mysql.db.server}:3306/mydatabase?useSSL=false

  • Nesting property expressions:

    You can compose property expressions by nesting a property expression inside another property expression. When nested property expressions are expanded, the inner expression is expanded first:

    ${<outer_property_expression>${<inner_property_expression>}}
  • Multiple property expressions:

    You can join two or more property expression together as shown below:

    ${<first_property>}${<second_property>}
  • Combining property expressions with environment variables:

    You can use property expressions to substitute the values of environment variables. The expression in the following example substitutes the value that is set for the HOST environment variable as the value of the application.host property. When HOST environment variable is not set, application.host uses the value of the remote.host property as the default:

    application.properties

    remote.host=quarkus.io
    application.host=${HOST:${remote.host}}