Chapter 5. Accessing the configuration from code

You can access the configuration by using a method defined in your code. You can achieve dynamic lookups or retrieve configured values from classes that are either CDI beans or JAX-RS resources.

You can access the configuration using the org.eclipse.microprofile.config.ConfigProvider.getConfig() method. The getValue method of the Config object returns the values of the configuration properties.

Prerequisites

  • You have a Quarkus Maven project.

Procedure

  • Access the configuration using one of the following options:

    • To access a configuration of a property that is defined already in your application.properties file, use the following syntax where DATABASE.NAME is the name of a property that is assigned to a databaseName variable:

      String databaseName = ConfigProvider.getConfig().getValue("DATABASE.NAME", String.class);
    • To access a configuration of a property that might not be defined in your application.properties file, use the following syntax:

      Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("DATABASE.NAME", String.class);