Skip to content

Commit 5e1c0cc

Browse files
authored
Merge pull request quarkusio#49764 from radcortez/config-docs
Update Configuration Reference doc
2 parents 29c675d + 98f47d9 commit 5e1c0cc

File tree

1 file changed

+126
-42
lines changed

1 file changed

+126
-42
lines changed

docs/src/main/asciidoc/config-reference.adoc

Lines changed: 126 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ include::_attributes.adoc[]
1212
:sectnumlevels: 4
1313
:topics: configuration
1414

15-
IMPORTANT: The content of this guide has been revised and split into additional topics. Please check the <<additional-information,Additional Information>> section.
16-
1715
In this reference guide we're going to describe various aspects of Quarkus configuration. A Quarkus application and
1816
Quarkus itself (core and extensions) are both configured via the same mechanism that leverages
1917
the https://github.com/smallrye/smallrye-config[SmallRye Config] API an implementation of the
@@ -195,9 +193,80 @@ Quarkus provides additional extensions which cover other configuration formats a
195193

196194
TIP: It is also possible to create a xref:config-extending-support.adoc#custom-config-source[Custom Config Source].
197195

198-
== Inject
196+
== ConfigMapping
197+
198+
The recommended way to expose configuration to a Quarkus application is by mapping it with an interface. With
199+
https://smallrye.io/smallrye-config/Main/config/mappings/[SmallRye Config Mappings], it is possible to
200+
group multiple configuration properties in a single interface that share the same prefix (or namespace). It supports
201+
the following set of features:
202+
203+
* Automatic conversion of the configuration type, including List, Set, Map, Optional, and primitive types.
204+
* Nested Config Mapping groups.
205+
* Configuration Properties Naming Strategies
206+
* Integration with Bean Validation
199207

200-
Quarkus uses https://microprofile.io/project/eclipse/microprofile-config[MicroProfile Config] annotations to inject the
208+
A Config Mapping requires an interface with minimal metadata configuration annotated with
209+
`@io.smallrye.config.ConfigMapping`:
210+
211+
[source,java]
212+
----
213+
@ConfigMapping(prefix = "server")
214+
public interface Server {
215+
String host();
216+
217+
int port();
218+
219+
int sslPort();
220+
}
221+
----
222+
223+
A complex object type uses the following rules to map configuration values to their member values:
224+
225+
* A configuration path is built by taking the object type prefix (or namespace) and the mapping member name
226+
* The member name is converted to its kebab-case format
227+
* If the member name is represented as a getter, the member name is taken from its property name equivalent, and then
228+
converted to its kebab-case format.
229+
* The configuration value is automatically converted to the member type
230+
* The configuration path is required to exist with a valid configuration value or the mapping will fail.
231+
232+
NOTE: Kebab-case - the method name is derived by replacing case changes with a dash to map the configuration property.
233+
234+
The `Server` interface can map configuration properties with the name `server.host` into the `Server.host()`
235+
method, `server.port` into `Server.port()` method, and `server.ssl-port` into `Server.sslPort()` method. The
236+
configuration property name to look up is constructed from the prefix and the method name (in kebab-case), separated
237+
by adot (`.`) character.
238+
239+
A `@ConfigMapping` can be injected into any CDI aware bean:
240+
241+
[source,java]
242+
----
243+
@ApplicationScoped
244+
class BusinessBean {
245+
@Inject
246+
Server server;
247+
248+
void businessMethod() {
249+
String host = server.host();
250+
}
251+
}
252+
----
253+
254+
For non-CDI components, use the `io.smallrye.config.SmallRyeConfig#getConfigMapping` API to retrieve the config mapping
255+
instance:
256+
257+
[source,java]
258+
----
259+
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
260+
Server server = config.getConfigMapping(Server.class);
261+
----
262+
263+
TIP: Check the xref:config-mappings.adoc[Mapping configuration to objects] guide for advanced usages with
264+
`@ConfigMapping`.
265+
266+
== ConfigProperty
267+
268+
While `@ConfigMapping` is the recommended approach, Quarkus also supports
269+
https://microprofile.io/project/eclipse/microprofile-config[MicroProfile Config] annotations to inject the
201270
configuration properties in the application.
202271

203272
[source,java]
@@ -221,19 +290,20 @@ String suffix;
221290
@ConfigProperty(name = "greeting.name")
222291
Optional<String> name; <3>
223292
----
224-
<1> If you do not provide a value for this property, the application startup fails with `jakarta.enterprise.inject.spi.DeploymentException: No config value of type [class java.lang.String] exists for: greeting.message`.
293+
<1> If you do not provide a value for this property, the application startup fails with
294+
`io.quarkus.runtime.configuration.ConfigurationException: Failed to load config value of type class java.lang.String for: greeting.messagee`.
225295
<2> The default value is injected if the configuration does not provide a value for `greeting.suffix`.
226296
<3> This property is optional - an empty `Optional` is injected if the configuration does not provide a value for `greeting.name`.
227297

228298
TIP: Use xref:config-mappings.adoc#config-mappings[Config Mappings] to group similar configuration properties.
229299

230-
=== Default Values
300+
== Default Values
231301

232-
If a property is associated with a default value (by way of the `defaultValue` attribute), and no configuration value
233-
is supplied for the property, then rather than throwing a `jakarta.enterprise.inject.spi.DeploymentException`, the
234-
default value will be used. The `defaultValue` value is expressed as a `String`, and uses the same conversion mechanism
235-
used to process configuration values. Several Built-in Converters already exist for primitives, boxed primitives, and
236-
other classes; for example:
302+
If a configuration is associated with a default value (by way of the `io.smallrye.config.WithDefault` or
303+
`org.eclipse.microprofile.config.inject.ConfigProperty.defaultValue`), and no configuration value is supplied for the
304+
property, then rather than throwing an exception, the default value will be used. A default value is expressed as a
305+
`String`, and uses the same conversion mechanism used to process configuration values. Several Built-in Converters
306+
already exist for primitives, boxed primitives, and other classes; for example:
237307

238308
* Primitives: `boolean`, `byte`, `short`, etc.
239309
* Boxed primitives: `java.lang.Boolean`, `java.lang.Byte`, `java.lang.Short`, etc.
@@ -248,10 +318,10 @@ these converters comply with the Microprofile or custom implementation providers
248318
* Boolean values will be `true` in cases "true", "1", "YES", "Y" "ON". Otherwise, value will be interpreted as false
249319
* For float and double values the fractional digits must be separated by a dot `.`
250320

251-
Note that when a combination of `Optional*` types and the `defaultValue` attribute are used, the defined `defaultValue`
252-
will still be used and if no value is given for the property, the `Optional*` will be present and populated with the
253-
converted default value. However, when the property is explicitly empty, the default value is not used and the
254-
`Optional` will be empty. Consider this example:
321+
Note that when a combination of `Optional*` types and defaults are used, the defined default value will still be used
322+
and if no value is given for the property, the `Optional*` will be present and populated with the converted default
323+
value. However, when the property is explicitly empty, the default value is not used and the `Optional` will be empty.
324+
Consider this example:
255325

256326
[source,properties]
257327
----
@@ -274,23 +344,16 @@ greeting.suffix=
274344
will result in a `java.util.NoSuchElementException: SRCFG02004: Required property greeting.message not found` on
275345
startup and the default value will not be assigned.
276346

277-
Below is an example of a Quarkus-supplied converter:
278-
279-
[source,java]
280-
----
281-
@ConfigProperty(name = "server.address", defaultValue = "192.168.1.1")
282-
InetAddress serverAddress;
283-
----
284-
285347
== Programmatically access
286348

287-
The `org.eclipse.microprofile.config.ConfigProvider.getConfig()` API allows to access the Config API programmatically.
349+
The `io.smallrye.config.SmallRyeConfig` API allows to access the Config API programmatically.
288350
This API is mostly useful in situations where CDI injection is not available.
289351

290352
[source,java]
291353
----
292-
String databaseName = ConfigProvider.getConfig().getValue("database.name", String.class);
293-
Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("database.name", String.class);
354+
SmallRyeConfig config = org.eclipse.microprofile.config.ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
355+
String databaseName = config.getValue("database.name", String.class);
356+
Optional<String> maybeDatabaseName = config.getOptionalValue("database.name", String.class);
294357
----
295358

296359
IMPORTANT: Do not use `System.getProperty(String)` or `System.getEnv(String)` to retrieve configuration values. These
@@ -496,17 +559,6 @@ Multiple profiles priority work in reverse order. With `quarkus.profile=common,d
496559
profile and then the `common` profile.
497560
====
498561

499-
=== Default Runtime Profile
500-
501-
The default Quarkus runtime profile is set to the profile used to build the application:
502-
503-
[source,bash]
504-
----
505-
./mvnw package -Dnative -Dquarkus.profile=prod-aws
506-
./target/my-app-1.0-runner // <1>
507-
----
508-
<1> The command will run with the `prod-aws` profile. This can be overridden using the `quarkus.profile` configuration.
509-
510562
[[property-expressions]]
511563
== Property Expressions
512564

@@ -674,19 +726,51 @@ extensions. Therefore, the `quarkus.` prefix should **never** be used for applic
674726

675727
=== Build Time configuration
676728

677-
Some Quarkus configurations only take effect during build time, meaning it is not possible to change them at runtime. These
678-
configurations are still available at runtime but as read-only and have no effect in Quarkus behaviour. A change to any
679-
of these configurations requires a rebuild of the application itself to reflect changes of such properties.
729+
Some Quarkus configurations only take effect during build time (compile), meaning it is not possible to change it at
730+
runtime. These configurations are still available at runtime but as read-only and have no effect in Quarkus
731+
behaviour. A change to any of these configurations requires a rebuild of the application itself to reflect changes of
732+
such properties. This allows Quarkus to provide optimizations and reduce complexity based on the current build
733+
configuration. By default, Quarkus uses the `prod` configuration profile to build the application.
680734

681735
TIP: The properties fixed at build time are marked with a lock icon (icon:lock[]) in the xref:all-config.adoc[list of all configuration options].
682736

683737
However, some extensions do define properties _overridable at runtime_. A simple example is the database URL, username
684738
and password which is only known specifically in your target environment, so they can be set and influence the
685739
application behaviour at runtime.
686740

687-
== Change build time properties after your application has been published
741+
=== Config Recording
742+
743+
During the build process, Quarkus proceeds to record the available configuration in the final binary, to ensure it can
744+
start successfully in runtime, without missing required configuration. The following sources are excluded from
745+
recording:
746+
747+
* System Properties
748+
* Environment Variables
749+
* `.env`
750+
* Build System Sources (provided by Maven or Gradle)
751+
752+
=== Default Runtime Profile
753+
754+
The default Quarkus runtime profile is set to the profile used to build the application:
755+
756+
[source,bash]
757+
----
758+
./mvnw package -Dnative -Dquarkus.profile=prod-aws
759+
./target/my-app-1.0-runner // <1>
760+
----
761+
<1> The command will run with the `prod-aws` profile. This can be overridden using the `quarkus.profile` configuration.
762+
763+
[IMPORTANT]
764+
====
765+
Using a different configuration profile at runtime from the one used to build the application can lead to unexpected
766+
results.
767+
====
768+
769+
== Change configuration after build (Reaugmentation)
688770

689-
If you are in the rare situation that you need to change the build time configuration after your application is built, then check out how xref:reaugmentation.adoc[re-augmentation] can be used to rebuild the augmentation output for a different build time configuration.
771+
If you are in the rare situation that you need to change the build time configuration after your application is built,
772+
then check out how xref:reaugmentation.adoc[re-augmentation] can be used to rebuild the augmentation output for a
773+
different build time configuration.
690774

691775
== Tracking effective build time configuration used at build time
692776

0 commit comments

Comments
 (0)