Creating a Configuration File

Default Quarkus reads application.properties. In the following content editing SRC/main/resources/application. The properties

`# Your configuration properties`
`greeting.message = hello`
`greeting.name = quarkus`
Copy the code

Inject configuration items

Quarkus uses MicroProfile Config to inject the configuration into the application. Injection uses the @configProperty annotation

`@ConfigProperty(name = "greeting.message")` `String message; `Copy the code

The @inject @configProperty annotation is not required for members annotated with @ConfigProperty. This is different from MicroProfile Config

Edit org. Acme. Config. GreetingResource and add the following configuration properties:

`@ConfigProperty(name = "greeting.message")` `String message; ` `@ConfigProperty(name = "greeting.suffix", defaultValue="!" )` `String suffix; ` `@ConfigProperty(name = "greeting.name")` `Optional<String> name; `Copy the code
  • If there is no configuration values for attributes, the application will start failure error javax.mail. Enterprise. Inject. Spi. DeploymentException: No config value of type [class java.lang.String] exists for: greeting.message.
  • If greeting.suffix values are not configured, default values are injected.
  • Optional property – The greeting.name value is injected optional.empty () if not configured.

Modify the Hello method to use injected properties:

`@GET` `@Produces(MediaType.TEXT_PLAIN)` `public String hello() {` `return message + " " + name.orElse("world") + suffix; ` ` `}Copy the code

Once set, check in the following ways:

`$ curl http://localhost:8080/greeting` `hello quarkus! `Copy the code

Into multiple related configuration values of alternative, you can also use the @. IO quarkus. Arc. Config. ConfigProperties comments to these attributes together.

Update and Test

We also need to adjust functional testing accordingly. Edit the SRC/test/Java/org/acme/config/GreetingResourceTest Java file modification method of testHelloEndpoint content as follows:

`package org.acme.config; ` `import io.quarkus.test.junit.QuarkusTest; ` `import org.junit.jupiter.api.Test; ` `import static io.restassured.RestAssured.given; ` `import static org.hamcrest.CoreMatchers.is; ` `@QuarkusTest` `public class GreetingResourceTest {` `@Test` `public void testHelloEndpoint() {` `given()` `.when().get("/greeting")` `.then()` `.statusCode(200)` `.body(is("hello quarkus!" )); // Modified line` `}` `}`Copy the code

Package and run the application

Run the application with./ MVNW compile quarkus:dev. Using the browser open http://localhost:8080/greeting.

You can see the changes immediately after you modify the configuration file. You can add greeting.suffix, delete other attributes, change values, and so on.

In general, applications can be packaged with a./ MVNW clean package and run with a -runner. Jar file. You can also generate native executables with./ MVNW Clean package-pnative.

Programmatic access configuration

Configuration can be accessed programmatically. It can be convenient to implement dynamic lookup or retrieval of configured values from classes that are neither CDI beans nor JAX-RS resources.

Can use the following ways to programmatically access configuration org. The eclipse microprofile. Config. ConfigProvider. GetConfig () :

`String databaseName = ConfigProvider.getConfig().getValue("database.name", String.class); ` `Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("database.name", String.class); `Copy the code

Configuration Profiles

Quarkus supports the concept of configuring profiles. These allow you to have multiple configuration values in the same file and choose between them by profile name.

The syntax is %{profile}.config.key=value. For example, if I have the following:

`quarkus.http.port=9090`
`%dev.quarkus.http.port=8181`
Copy the code

Then, unless the dev profile is active, the Quarkus HTTP port will be 9090, in which case it will be 8181.

Configuration Quarkus

Quarkus itself is configured through the same mechanism as the application. Quarkus retains the Quarkus. Namespace. For example, in application.properties, set the HTTP server port

The attribute prefix quarkus. Is effectively reserved for configuring Quarkus itself, hence quarkus. Should never be used as a prefix application-specific attribute.

Quarkus does most of the configuration and boot at build time, and some configuration properties are read and used during build. These attributes _ are _ fixed _ at build time and cannot be changed at run time. Applications always need to be repackaged to reflect changes to such attributes.

Properties fixed at build time are marked with a lock icon () in the list of all configuration options.

However, some extensions do define an _ attribute that can be overridden at runtime. A typical example is the database URL, username, and password, which are only known in the target environment.

  1. System property
  2. The environment variable
  3. Env is placed in the current working directory
  4. Placed in the configuration file $PWD/config/application properties