Este conteúdo não está disponível no idioma selecionado.
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 Copiar o linkLink copiado para a área de transferência!
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}/remote.host=quarkus.io callable.url=https://${remote.host}/Copy to Clipboard Copied! Toggle word wrap Toggle overflow The value of the
callable.urlproperty resolves tohttps://quarkus.io/.Setting a property value that is specific to a particular configuration profile:
In the following example, the
%devconfiguration 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
%dev.quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false quarkus.datasource.jdbc.url=jdbc:mysql://remotehost:3306/mydatabase?useSSL=falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can achieve the same result in a simplified way by setting a different value of the custom
application.serverproperty 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%dev.application.server=localhost application.server=remotehost quarkus.datasource.jdbc.url=jdbc:mysql://${application.server}:3306/mydatabase?useSSL=falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
application.serverproperty 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>}${<expression>:<default_value>}Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the following example, the property expression in the data source URL uses
mysql.db.serveras the default value of theapplication.serverproperty:application.properties
quarkus.datasource.jdbc.url=jdbc:mysql://${application.server:mysql.db.server}:3306/mydatabase?useSSL=falsequarkus.datasource.jdbc.url=jdbc:mysql://${application.server:mysql.db.server}:3306/mydatabase?useSSL=falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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>}}${<outer_property_expression>${<inner_property_expression>}}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Multiple property expressions:
You can join two or more property expression together as shown below:
${<first_property>}${<second_property>}${<first_property>}${<second_property>}Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
HOSTenvironment variable as the value of theapplication.hostproperty. WhenHOSTenvironment variable is not set,application.hostuses the value of theremote.hostproperty as the default:application.properties
remote.host=quarkus.io application.host=${HOST:${remote.host}}remote.host=quarkus.io application.host=${HOST:${remote.host}}Copy to Clipboard Copied! Toggle word wrap Toggle overflow