Este contenido no está disponible en el idioma seleccionado.
Chapter 4. Injecting configuration values into your Quarkus application
Red Hat build of Quarkus uses the MicroProfile Config feature to inject configuration data into the application. You can access the configuration through context and dependency injection (CDI) or by using a method defined in your code.
You can use the @ConfigProperty annotation to map an object property to a key in the MicroProfile ConfigSources file of your application. This procedure shows you how to inject an individual property configuration into a Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
-
Open the
src/main/resources/application.propertiesfile. Add configuration properties to your configuration file where
<key>is the property name and<value>is the value of the property:<key>=<value>The following example shows how to set the values for the
greeting.messageand thegreeting.nameproperties in the Quarkusconfig-quickstartproject:greeting.message = hello greeting.name = quarkusImportantUse
quarkusas a prefix to Quarkus properties.Review the
GreetingResource.javafile and make sure it includes the following import statements:import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;Define the equivalent properties by annotating them with the following syntax:
@ConfigProperty(name = "greeting.message")1 String message; @ConfigProperty(name = "greeting.suffix", defaultValue="!")2 String suffix; @ConfigProperty(name = "greeting.name") Optional<String> name;3 - 1
- If you do not provide a value for this property, the application will fail and throw the following exception message:
javax.enterprise.inject.spi.DeploymentException: No config value of type [class java.lang.String] exists for: greeting.message - 2
- If you do not provide a value for the
greeting.suffix, Quarkus resolves it to the default value. - 3
- If the
Optionalparameter does not have a value, it returns no value forgreeting.name.
NoteTo inject a configured value, you can use
@ConfigProperty. The@Injectannotation is not necessary for members annotated with@ConfigProperty.Edit your
hellomethod to return the following message:@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; }To compile your Quarkus application in development mode, enter the following command from the project directory:
./mvnw quarkus:devTo verify that the endpoint returns the message, enter the following command in a new terminal window:
curl http://localhost:8080/greetingThis command returns the following output:
hello quarkus!- To stop the application, press CTRL+C
4.1. Annotating a class with @ConfigProperties Copiar enlaceEnlace copiado en el portapapeles!
As an alternative to injecting multiple related configuration values individually, you can use the @io.quarkus.arc.config.ConfigProperties annotation to group configuration properties. The following procedure demonstrates the use of @ConfigProperties annotation on the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Review the
GreetingResource.javafile and make sure it includes the following import statements:package org.acme.config; import java.util.Optional; import javax.inject.Inject;-
Create a file
GreetingConfiguration.javain thesrc/main/java/org/acme/configdirectory. Add the
@ConfigPropertiesand@Optionalimports to theGreetingConfiguration.javafile:package org.acme.config; import io.quarkus.arc.config.ConfigProperties; import java.util.Optional;Create a
GreetingConfigurationclass for thegreetingproperties in yourGreetingConfiguration.javafile:@ConfigProperties(prefix = "greeting")1 public class GreetingConfiguration { private String message; private String suffix = "!";2 private Optional<String> name; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public Optional<String> getName() { return name; } public void setName(Optional<String> name) { this.name = name; } }Inject the attribute into the
GreetingResourceclass using the context and dependency injection (CDI)@Injectannotation:@Inject GreetingConfiguration greetingConfiguration;To compile your application in development mode, enter the following command from the project directory:
./mvnw quarkus:devImportantIf you do not provide values for the class properties, the application fails and a
javax.enterprise.inject.spi.DeploymentExceptionis thrown indicating a missing value. This does not apply toOptionalfields and fields with a default value.
4.2. Using nested object configuration Copiar enlaceEnlace copiado en el portapapeles!
You can define a nested class inside an existing class. This procedure demonstrates how to create a nested class configuration in the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Review the
GreetingConfiguration.javafile and make sure it includes the following import statements:import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import java.util.List;Add the configuration in your
GreetingConfiguration.javafile using the@ConfigPropertiesannotation.The following example shows the configuration of the
GreetingConfigurationclass and its properties:@ConfigProperties(prefix = "greeting") public class GreetingConfiguration { public String message; public String suffix = "!"; public Optional<String> name; }Add a nested class configuration similar to the following example:
@ConfigProperties(prefix = "greeting") public class GreetingConfiguration { public String message; public String suffix = "!"; public Optional<String> name; public HiddenConfig hidden; public static class HiddenConfig { public Integer prizeAmount; public List<String> recipients; } }This example shows a nested class
HiddenConfig. The name of the field, in this casehidden, determines the name of the properties bound to the object.Add the equivalent configuration properties to your
application.propertiesfile.The following example shows the value of properties for the
GreetingConfigurationandHiddenConfigclasses:greeting.message = hello greeting.name = quarkus greeting.hidden.prize-amount=10 greeting.hidden.recipients=Jane,JohnTo compile your application in development mode, enter the following command from the project directory:
./mvnw quarkus:dev
Classes annotated with @ConfigProperties can be annotated with Bean Validation annotations similar to the following example:
@ConfigProperties(prefix = "greeting")
public class GreetingConfiguration {
@Size(min = 20)
public String message;
public String suffix = "!";
}
Your project must include the quarkus-hibernate-validator dependency.
4.3. Annotating an interface with @ConfigProperties Copiar enlaceEnlace copiado en el portapapeles!
An alternative method for managing properties is to define them as an interface. If you annotate an interface with @ConfigProperties, the interface can extend other interfaces, and you can use methods from the entire interface hierarchy to bind properties.
This procedure shows an implementation of the GreetingConfiguration class as an interface in the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Review the
GreetingConfiguration.javafile and make sure it includes the following import statements:package org.acme.config; import io.quarkus.arc.config.ConfigProperties; import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;Add a
GreetingConfigurationclass as an interface to yourGreetingConfiguration.javafile:@ConfigProperties(prefix = "greeting") public interface GreetingConfiguration { @ConfigProperty(name = "message")1 String message(); @ConfigProperty(defaultValue = "!") String getSuffix();2 Optional<String> getName();3 }- 1
- You must set the
@ConfigPropertyannotation because the name of the configuration property does not follow the getter method naming conventions. - 2
- In this example,
namewas not set so the corresponding property will begreeting.suffix. - 3
- You do not need to specify the
@ConfigPropertyannotation because the method name follows the getter method naming conventions (greeting.namebeing the corresponding property) and no default value is needed.
To compile your application in development mode, enter the following command from the project directory:
./mvnw quarkus:devImportantIf you do not provide a value for an interface field, the application fails and an
javax.enterprise.inject.spi.DeploymentExceptionis thrown indicating a missing value. This does not apply toOptionalfields and fields with a default value.