Este contenido no está disponible en el idioma seleccionado.
Chapter 8. Setting custom configuration sources
By default, Quarkus reads properties from the application.properties file. However, because Quarkus supports the MicroProfile Config feature, you can introduce custom configuration sources and load a configuration from another source.
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 on your Quarkus project.
Prerequisite
-
You have created the Quarkus
config-quickstartproject.
Procedure
Create an
ExampleConfigSourceProvider.javafile within your project and add the following imports: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;Create a class implementing the
ConfigSourceProviderinterface and make sure to override itsgetConfigSourcesmethod to return a list ofConfigSourceobjects:The following example shows an implementation of a custom
ConfigSourceProviderand aConfigSourceclasses: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; } } }-
Create the
org.eclipse.microprofile.config.spi.ConfigSourceProviderservice file in theMETA-INF/services/directory. Open the
org.eclipse.microprofile.config.spi.ConfigSourceProviderfile and add the fully qualified name of your custom ConfigSourceProvider class:org.acme.config.ExampleConfigSourceProviderTo compile the application in development mode, enter the following command from the project directory:
./mvnw quarkus:devWhen you restart your application, Quarkus picks up the custom configuration provider.