官术网_书友最值得收藏!

Custom ConfigSources implementations

It is possible to provide additional sources of configuration in your application that will be automatically added by the MicroProfile Config implementation.

You need to define an implementation of org.eclipse.microprofile.config.spi.ConfigSource and add a Java ServiceLoader configuration for it, and put that file in your application archive as META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource. For your reference, here is an example of the definition of an implementation of an environment ConfigSource:

package io.packt.sample.config;

import java.io.Serializable;
import java.util.Collections;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigSource;

public class EnvConfigSource implements ConfigSource, Serializable {

EnvConfigSource() {
}

@Override
public Map<String, String> getProperties() {
return Collections.unmodifiableMap(System.getenv());
}

@Override
public int getOrdinal() {
return 300;
}

@Override
public String getValue(String name) {
if (name == null) {
return null;
}

// exact match
String value = System.getenv(name);
if (value != null) {
return value;
}

// replace non-alphanumeric characters by underscores
name = name.replaceAll("[^a-zA-Z0-9_]", "_");

value = System.getenv(name);
if (value != null) {
return value;
}

// replace non-alphanumeric characters by underscores and convert
// to uppercase
return System.getenv(name.toUpperCase());
}

@Override
public String getName() {
return "EnvConfigSource";
}
}

In addition to providing additional ConfigSource, the MicroProfile Config API allows users to convert raw config property values into application-specific objects using converters, as described in the next section.

主站蜘蛛池模板: 横山县| 思南县| 施甸县| 依兰县| 突泉县| 榆中县| 奎屯市| 玛纳斯县| 宁河县| 石屏县| 忻城县| 古田县| 建平县| 合作市| 禄丰县| 曲沃县| 文昌市| 龙井市| 新源县| 南涧| 白玉县| 黔西| 淄博市| 凤山市| 专栏| 万载县| 榆林市| 印江| 霍城县| 刚察县| 通渭县| 崇文区| 司法| 巴楚县| 梨树县| 湾仔区| 蒲城县| 宁陕县| 南岸区| 汕尾市| 丘北县|