You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Add new section about ConfigMapping
- Renamed some sections for consistency
- Generic Defaults description
- Add new section about Config Recording
- Prefer to user SmallRyeConfig instead of MP Config
IMPORTANT: The content of this guide has been revised and split into additional topics. Please check the <<additional-information,Additional Information>> section.
16
-
17
15
In this reference guide we're going to describe various aspects of Quarkus configuration. A Quarkus application and
18
16
Quarkus itself (core and extensions) are both configured via the same mechanism that leverages
19
17
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
195
193
196
194
TIP: It is also possible to create a xref:config-extending-support.adoc#custom-config-source[Custom Config Source].
197
195
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
199
207
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
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
201
270
configuration properties in the application.
202
271
203
272
[source,java]
@@ -221,19 +290,20 @@ String suffix;
221
290
@ConfigProperty(name = "greeting.name")
222
291
Optional<String> name; <3>
223
292
----
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`.
225
295
<2> The default value is injected if the configuration does not provide a value for `greeting.suffix`.
226
296
<3> This property is optional - an empty `Optional` is injected if the configuration does not provide a value for `greeting.name`.
227
297
228
298
TIP: Use xref:config-mappings.adoc#config-mappings[Config Mappings] to group similar configuration properties.
229
299
230
-
=== Default Values
300
+
== Default Values
231
301
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:
237
307
238
308
* Primitives: `boolean`, `byte`, `short`, etc.
239
309
* 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
248
318
* Boolean values will be `true` in cases "true", "1", "YES", "Y" "ON". Otherwise, value will be interpreted as false
249
319
* For float and double values the fractional digits must be separated by a dot `.`
250
320
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:
255
325
256
326
[source,properties]
257
327
----
@@ -274,23 +344,16 @@ greeting.suffix=
274
344
will result in a `java.util.NoSuchElementException: SRCFG02004: Required property greeting.message not found` on
275
345
startup and the default value will not be assigned.
276
346
277
-
Below is an example of a Quarkus-supplied converter:
<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)
688
770
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.
690
774
691
775
== Tracking effective build time configuration used at build time
0 commit comments