第9章 カスタム設定ソースの設定

デフォルトで、Quarkus アプリケーションはプロジェクトの src/main/resources サブディレクトリー内の application.properties ファイルからプロパティーを読み取ります。ただし、Quarkus は MicroProfile 設定をサポートしているため、他のソースからアプリケーションの設定を読み込むこともできます。org.eclipse.microprofile.config.spi.ConfigSource および org.eclipse.microprofile.config.spi.ConfigSourceProvider インターフェイスを実装するクラスを提供することで、設定した値にカスタム設定ソースを導入できます。以下の手順では、Quarkus プロジェクトにカスタム設定ソースを実装する方法を説明します。

前提条件

  • Quarkus config-quickstart プロジェクトがある。

手順

  1. プロジェクト内に ExampleConfigSourceProvider.java ファイルを作成し、以下のインポートを追加します。

    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. org.eclipse.microprofile.config.spi.ConfigSourceProvider インターフェイスを実装するクラスを作成します。その getConfigSources メソッドを上書きし、ConfigSource オブジェクトの一覧を返します。

    以下の例は、ConfigSourceProvider および ConfigSource インターフェイスのカスタム実装を示しています。

    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. プロジェクトの src/main/resources/META-INF/services/ サブディレクトリーに org.eclipse.microprofile.config.spi.ConfigSourceProvider という名前のファイルを作成し、作成したファイルに ConfigSourceProvider を実装するクラスの完全修飾名をファイルに入力します。

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

    org.acme.config.ExampleConfigSourceProvider

    アプリケーションをコンパイルして起動する際に、作成した ConfigSourceProvider が登録およびインストールされていることを確認するには、この手順を実行する必要があります。

  4. 以下のコマンドを入力して、開発モードでアプリケーションをコンパイルして起動します。

    ./mvnw quarkus:dev
  5. /greeting エンドポイントが想定されるメッセージを返すことを確認するには、新しいターミナルウィンドウに以下のコマンドを入力します。

    curl http://localhost:8080/greeting
  6. アプリケーションがカスタム設定を正しく読み取ると、以下の応答が返されます。

    hello quarkus!