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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.ExampleConfigSourceProvider
org.acme.config.ExampleConfigSourceProviderCopy to Clipboard Copied! Toggle word wrap Toggle overflow You 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:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command in a new terminal window to verify that the
/greetingendpoint returns the expected message:curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow When your application reads the custom configuration properly, you receive the following response.
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow