If a property is associated with a default value (by way of the
defaultValue
attribute), and no configuration value
is supplied for the property, then rather than throwing a
jakarta.enterprise.inject.spi.DeploymentException
, the
default value will be used. The
defaultValue
value is expressed as a
String
, and uses the same conversion mechanism
used to process configuration values. Several Built-in Converters already exist for primitives, boxed primitives, and
other classes; for example:
Optional containers:
java.util.Optional
,
java.util.OptionalInt
,
java.util.OptionalLong
, and
java.util.OptionalDouble
Java
enum
types
JSR 310
java.time.Duration
JDK networking
java.net.SocketAddress
,
java.net.InetAddress
, etc.
As you might expect, these converters are
org.eclipse.microprofile.config.spi.Converter
implementations. Therefore,
these converters comply with the Microprofile or custom implementation providers expression rules like:
Boolean values will be
true
in cases "true", "1", "YES", "Y" "ON". Otherwise, value will be interpreted as false
For float and double values the fractional digits must be separated by a dot
.
Note that when a combination of
Optional*
types and the
defaultValue
attribute are used, the defined
defaultValue
will still be used and if no value is given for the property, the
Optional*
will be present and populated with the
converted default value. However, when the property is explicitly empty, the default value is not used and the
Optional
will be empty. Consider this example:
# missing value, optional property
greeting.name=
In this case, since
greeting.name
was configured to be
Optional*
up above, the corresponding property value will
be an empty
Optional
and execution will continue normally. This would be the case even if there was a default value
configured: the default value is
not
used if the property is explicitly cleared in the configuration.
On the other hand, this example:
# missing value, non-optional
greeting.suffix=
will result in a
java.util.NoSuchElementException: SRCFG02004: Required property greeting.message not found
on
startup and the default value will not be assigned.
Below is an example of a Quarkus-supplied converter:
@ConfigProperty(name = "server.address", defaultValue = "192.168.1.1")
InetAddress serverAddress;
The
org.eclipse.microprofile.config.ConfigProvider.getConfig()
API allows to access the Config API programmatically.
This API is mostly useful in situations where CDI injection is not available.
String databaseName = ConfigProvider.getConfig().getValue("database.name", String.class);
Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("database.name", String.class);
Do not use
System.getProperty(String)
or
System.getEnv(String)
to retrieve configuration values. These
APIs are not configuration aware and do not support the features described in this guide.
We often need to configure our application differently depending on the target
environment
. For example, the local
development environment may be different from the production environment.
Configuration Profiles allow for multiple configurations in the same file or separate files and select between them via
a profile name.
To be able to set properties with the same name, each property needs to be prefixed with a percentage sign
%
followed
by the profile name and a dot
.
in the syntax
%{profile-name}.config.name
:
application.properties
quarkus.http.port=9090
%dev.quarkus.http.port=8181
The Quarkus HTTP port will be 9090. If the
dev
profile is active it will be 8181.
Profiles in the
.env
file follow the syntax
_{PROFILE}_CONFIG_KEY=value
:
QUARKUS_HTTP_PORT=9090
_DEV_QUARKUS_HTTP_PORT=8181
If a profile does not define a value for a specific attribute, the
default
(no profile) value is used:
application.properties
bar=”hello”
baz=”bonjour”
%dev.bar=”hallo”
With the
dev
profile enabled, the property
bar
has the value
hallo
, but the property
baz
has the value
bonjour
. If the
prod
profile is enabled,
bar
has the value
hello
(as there is no specific value for the
prod
profile), and
baz
the value
bonjour
.
In this case, properties for a specific profile may reside in an
application-{profile}.properties
named file. The previous
example may be expressed as:
application.properties
quarkus.http.port=9090
%staging.quarkus.http.test-port=9091
application-staging.properties
quarkus.http.port=9190
quarkus.http.test-port=9191
In this style, the configuration names in the profile aware file do not need to be prefixed with the profile name.
Properties in the profile aware file have priority over profile aware properties defined in the main file.
Do not use profile aware files to set
quarkus.profile
or
quarkus.test.profile
. This will not work because the
profile is required in advance to load the profile aware files.
A profile aware file is only loaded if the unprofiled
application.properties
is also available in the same location
and the file extension matches between the files. This is required to keep a consistent loading order and pair all the
resources together.
Multiple Profiles may be active at the same time. The configuration
quarkus.profile
accepts a comma-separated list
of profile names:
quarkus.profile=common,dev
. Both
common
and
dev
are separate profiles.
When multiple profiles are active, the rules for profile configuration are the same. If two profiles define the same
configuration, then the last listed profile has priority. Consider:
application.properties
quarkus.profile=common,dev
my.prop=1234
%common.my.prop=1234
%dev.my.prop=5678
%common.commom.prop=common
%dev.dev.prop=dev
%test.test.prop=test
It is also possible to define multiple profile properties, with a comma-separated list of profile names. If the same
property name exists in multiple profile properties then, the property name with the most specific profile wins:
application.properties
quarkus.profile=dev
%prod,dev.my.prop=1234
%dev.my.prop=5678
%prod,dev.another.prop=1234
Multiple profiles priority work in reverse order. With
quarkus.profile=common,dev
, Quarkus first checks the
dev
profile and then the
common
profile.
Quarkus provides property expressions expansion on configuration values. An expression string is
a mix of plain strings and expression segments, which are wrapped by the sequence
${ … }
.
These expressions are resolved when the property is read. So if the configuration property is build time the property
expression will be resolved at build time. If the configuration property is overridable at runtime it will be resolved
at runtime.
Consider:
application.properties
remote.host=quarkus.io
callable.url=https://${remote.host}/