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

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.

主站蜘蛛池模板: 南皮县| 阜城县| 隆化县| 五家渠市| 南部县| 桃源县| 玛沁县| 花莲市| 灵石县| 牙克石市| 英山县| 陆良县| 北辰区| 普安县| 平原县| 长治市| 台江县| 德州市| 邢台市| 水富县| 灵川县| 洞头县| 哈密市| 尼勒克县| 滦南县| 邓州市| 克拉玛依市| 大连市| 罗田县| 富阳市| 乾安县| 密山市| 都安| 嘉义县| 武汉市| 南丹县| 阜宁县| 广饶县| 东乌珠穆沁旗| 和林格尔县| 杭锦后旗|