Chapter 9. Setting custom configuration sources

By default, your Quarkus application reads properties from the application.properties file in the src/main/resources subdirectory of your project. However, because Quarkus supports MicroProfile Config, you can also load the configuration of your application from other sources. You can introduce custom configuration sources for your configured values by providing classes that implement the org.eclipse.microprofile.config.spi.ConfigSource and the org.eclipse.microprofile.config.spi.ConfigSourceProvider interfaces. This procedure demonstrates how you can implement a custom configuration source in your Quarkus project.

Prerequisite

  • Have the Quarkus config-quickstart project.

Procedure

  1. Create an ExampleConfigSourceProvider.java file in your project and add the following imports:

    src/main/java/org/acme/config/ExampleConfigSourceProvider.java

    package org.acme.config;
    
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import org.eclipse.microprofile.config.spi.ConfigSource;
    import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

  2. Create a class that implements the org.eclipse.microprofile.config.spi.ConfigSourceProvider interface. You must override its getConfigSources method to return a list of ConfigSource objects:

    The following example shows a custom implementation of the ConfigSourceProvider and the ConfigSource interfaces:

    src/main/java/org/acme/config/ExampleConfigSourceProvider.java

    public class ExampleConfigSourceProvider implements ConfigSourceProvider {
    
        private final int times = 2;
        private final String name = "example";
        private final String value = "value";
    
        @Override
        public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {
            InMemoryConfigSource configSource = new InMemoryConfigSource(Integer.MIN_VALUE, "example config source");
            for (int i = 0; i < this.times; i++) {
                configSource.add(this.name + ".key" + (i + 1), this.value + (i + 1));
            }
            return Collections.singletonList(configSource);
        }
    
        private static final class InMemoryConfigSource implements ConfigSource {
    
            private final Map<String, String> values = new HashMap<>();
            private final int ordinal;
            private final String name;
    
            private InMemoryConfigSource(int ordinal, String name) {
                this.ordinal = ordinal;
                this.name = name;
            }
    
            public void add(String key, String value) {
                values.put(key, value);
            }
    
            @Override
            public Map<String, String> getProperties() {
                return values;
            }
    
            @Override
            public Set<String> getPropertyNames() {
                return values.keySet();
            }
    
            @Override
            public int getOrdinal() {
                return ordinal;
            }
    
            @Override
            public String getValue(String propertyName) {
                return values.get(propertyName);
            }
    
            @Override
            public String getName() {
                return name;
            }
        }
    }

  3. Create a file named org.eclipse.microprofile.config.spi.ConfigSourceProvider in the src/main/resources/META-INF/services/ subdirectory of your project, and enter the fully qualified name of the class that implements the ConfigSourceProvider in the file that you created:

    src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider

    org.acme.config.ExampleConfigSourceProvider

    You must perform this step to ensure that the ConfigSourceProvider that you created is registered and installed when you compile and start your application.

  4. Enter the following command to compile and start your application in development mode:

    ./mvnw quarkus:dev
  5. Enter the following command in a new terminal window to verify that the /greeting endpoint returns the expected message:

    curl http://localhost:8080/greeting
  6. When your application reads the custom configuration properly, you receive the following response.

    hello quarkus!