- Hands-On Enterprise Java Microservices with Eclipse MicroProfile
- Cesar Saavedra Heiko W. Rupp Jeff Mesnil Pavol Loffay Antoine Sabot Durand Scott Stark
- 208字
- 2021-06-24 12:56:21
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.
推薦閱讀
- Application Development with Qt Creator(Second Edition)
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- 面向物聯網的CC2530與傳感器應用開發
- 物聯網+BIM:構建數字孿生的未來
- 企業網絡安全管理
- 物聯網通信技術
- 中國互聯網發展報告2018
- Master Apache JMeter:From Load Testing to DevOps
- React Cookbook
- 組網技術與網絡管理
- TCP/IP基礎(第2版)
- 區塊鏈社區運營手冊
- 走近奇妙的物聯網
- ReasonML Quick Start Guide
- Hands-On Cloud:Native Microservices with Jakarta EE