이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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;
    Copy to Clipboard Toggle word wrap

  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;
            }
        }
    }
    Copy to Clipboard Toggle word wrap

  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
    Copy to Clipboard Toggle word wrap

    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
    Copy to Clipboard Toggle word wrap
  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
    Copy to Clipboard Toggle word wrap
  6. When your application reads the custom configuration properly, you receive the following response.

    hello quarkus!
    Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat