Este conteúdo não está disponível no idioma selecionado.
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-quickstartproject.
Procedure
Create an
ExampleConfigSourceProvider.javafile 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;Create a class that implements the
org.eclipse.microprofile.config.spi.ConfigSourceProviderinterface. You must override itsgetConfigSourcesmethod to return a list ofConfigSourceobjects:The following example shows a custom implementation of the
ConfigSourceProviderand theConfigSourceinterfaces: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; } } }Create a file named
org.eclipse.microprofile.config.spi.ConfigSourceProviderin thesrc/main/resources/META-INF/services/subdirectory of your project, and enter the fully qualified name of the class that implements theConfigSourceProviderin the file that you created:src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider
org.acme.config.ExampleConfigSourceProviderYou must perform this step to ensure that the
ConfigSourceProviderthat you created is registered and installed when you compile and start your application.Enter the following command to compile and start your application in development mode:
./mvnw quarkus:devEnter the following command in a new terminal window to verify that the
/greetingendpoint returns the expected message:curl http://localhost:8080/greetingWhen your application reads the custom configuration properly, you receive the following response.
hello quarkus!