Este conteúdo não está disponível no idioma selecionado.
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>
<key>=<value>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following example shows how to set the values for the
greeting.messageand thegreeting.nameproperties in the Quarkusconfig-quickstartproject:src/main/resources/application.properties
greeting.message = hello greeting.name = quarkus
greeting.message = hello greeting.name = quarkusCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantUse
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;
import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Define the corresponding properties by annotating them with
@ConfigPropertyas shown in the following example:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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:src/main/java/org/acme/config/GreetingResource.java
@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; }@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command returns the following output:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
4.1. Annotating a class with @ConfigProperties Copiar o linkLink copiado para a área de transferência!
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:src/main/java/org/acme/config/GreetingResource.java
import java.util.Optional; import javax.inject.Inject;
import java.util.Optional; import javax.inject.Inject;Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Create a file
GreetingConfiguration.javain thesrc/main/java/org/acme/configdirectory. Add the
ConfigPropertiesandOptionalimports to theGreetingConfiguration.javafile:src/main/java/org/acme/config/GreetingConfiguration.java
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import javax.inject.Inject;
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import javax.inject.Inject;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
GreetingConfigurationclass for thegreetingproperties in yourGreetingConfiguration.javafile:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Inject the
GreetingConfigurationclass into theGreetingResourceclass using the@Injectannotation:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf you do not provide values for the class properties, the application fails to compile and you receive a
javax.enterprise.inject.spi.DeploymentExceptionthat indicates a missing value. This does not apply toOptionalfields and fields with a default value.Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow You receive the following message:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
4.2. Using nested object configuration Copiar o linkLink copiado para a área de transferência!
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:src/main/java/org/acme/config/GreetingConfiguration.java
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import java.util.List;
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import java.util.List;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the configuration in your
GreetingConfiguration.javafile using the@ConfigPropertiesannotation.The following example shows the configuration of the
GreetingConfigurationclass and its properties:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a nested class inside the
GreetingConfigurationclass as shown in the following example:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example shows a nested class
ContentConfig. The name of the field, in this casecontent, determines the name of the properties bound to the object.Set the
greeting.content.prize-amountandgreeting.content.recipientsconfiguration properties in yourapplication.propertiesfile.The following example shows the values of properties for the
GreetingConfigurationandContentConfigclasses:src/main/resources/application.properties
greeting.message = hello greeting.name = quarkus greeting.content.prize-amount=10 greeting.content.recipients=Jane,John
greeting.message = hello greeting.name = quarkus greeting.content.prize-amount=10 greeting.content.recipients=Jane,JohnCopy to Clipboard Copied! Toggle word wrap Toggle overflow Inject the
GreetingConfigurationclass into theGreetingResourceclass using the@Injectannotation, and update the message string that the/greetingendpoint returns to have the message show the values that you set for the newgreeting.content.prize-amountandgreeting.content.recipientsproperties that you added:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf you do not provide values for the class properties, the application fails to compile and you receive a
javax.enterprise.inject.spi.DeploymentExceptionthat indicates a missing value. This does not apply toOptionalfields and fields with a default value.Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow You receive the message that contains the greeting on the first line and the recipients of the prize and the prize amount on the second line:
hello quarkus! Jane,John receive total of candies: 10
hello quarkus! Jane,John receive total of candies: 10Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
Classes annotated with @ConfigProperties can be annotated with Bean Validation annotations similar to the following example:
Your project must include the quarkus-hibernate-validator dependency.
4.3. Annotating an interface with @ConfigProperties Copiar o linkLink copiado para a área de transferência!
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:src/main/java/org/acme/config/GreetingConfiguration.java
import io.quarkus.arc.config.ConfigProperties; import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;
import io.quarkus.arc.config.ConfigProperties; import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a
GreetingConfigurationclass as an interface to yourGreetingConfiguration.javafile:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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.
Inject the
GreetingConfigurationclass into theGreetingResourceclass using the@Injectannotation:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf you do not provide values for the class properties, the application fails to compile and you receive a
javax.enterprise.inject.spi.DeploymentExceptionthat indicates a missing value. This does not apply toOptionalfields and fields with a default value.Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow You receive the following message:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.