Interface Config
- All Known Implementing Classes:
ContextAwareConfig
public interface Config
Unified Configuration Access Interface
统一配置访问接口
This interface provides type-safe configuration access with support for multiple sources, type conversion, placeholder resolution, and hot reloading.
此接口提供类型安全的配置访问,支持多配置源、类型转换、占位符解析和热更新。
Features | 主要功能:
- Type-safe configuration retrieval - 类型安全的配置检索
- Multiple configuration sources with priority - 支持优先级的多配置源
- Placeholder resolution ${key} - 占位符解析${key}
- Configuration change listeners - 配置变更监听
- Sub-configuration by prefix - 按前缀获取子配置
- Configuration binding to POJOs/Records - 配置绑定到POJO/Record
Usage Examples | 使用示例:
// Basic retrieval
String url = config.getString("database.url");
int port = config.getInt("server.port", 8080);
Duration timeout = config.getDuration("http.timeout");
// Type-safe retrieval
LocalDate date = config.get("start.date", LocalDate.class);
List<String> hosts = config.getList("redis.hosts", String.class);
// Optional retrieval
Optional<String> apiKey = config.getOptional("api.key");
// Sub-configuration
Config dbConfig = config.getSubConfig("database");
String dbUrl = dbConfig.getString("url"); // reads database.url
// Configuration binding
DatabaseConfig dbCfg = config.bind("database", DatabaseConfig.class);
// Change listeners
config.addListener("log.level", event -> {
System.out.println("Log level changed: " + event.newValue());
});
Performance | 性能特性:
- Time complexity: O(1) for direct key access - 时间复杂度: 直接键访问为O(1)
- Type conversion cached - 类型转换结果缓存
- Immutable snapshots for thread safety - 不可变快照保证线程安全
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Immutable interface - 不可变接口
- Since:
- JDK 25, opencode-base-config V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(ConfigListener listener) Add configuration change listener 添加配置变更监听器voidaddListener(String key, ConfigListener listener) Add listener for specific configuration key 添加指定键的监听器<T> TBind configuration to POJO/Record 绑定配置到POJO/Record<T> voidBind configuration to existing object 绑定配置到现有对象<T> TGet typed configuration value 获取指定类型配置值<T> TGet typed configuration value with default 获取指定类型配置值(带默认值)booleangetBoolean(String key) Get boolean configuration value 获取布尔配置值booleangetBoolean(String key, boolean defaultValue) Get boolean configuration value with default 获取布尔配置值(带默认值)getByPrefix(String prefix) Get all configurations with specified prefix 获取指定前缀的所有配置doubleGet double configuration value 获取双精度配置值doubleGet double configuration value with default 获取双精度配置值(带默认值)getDuration(String key) Get Duration configuration value 获取Duration配置值getDuration(String key, Duration defaultValue) Get Duration configuration value with default 获取Duration配置值(带默认值)intGet integer configuration value 获取整数配置值intGet integer configuration value with default 获取整数配置值(带默认值)getKeys()Get all configuration keys 获取所有配置键<T> List<T> Get list configuration value 获取列表配置值longGet long configuration value 获取长整数配置值longGet long configuration value with default 获取长整数配置值(带默认值)<K,V> Map <K, V> Get map configuration value 获取映射配置值getOptional(String key) Get optional string configuration value 获取Optional字符串配置值<T> Optional<T> getOptional(String key, Class<T> type) Get optional typed configuration value 获取Optional类型配置值Get string configuration value 获取字符串配置值Get string configuration value with default 获取字符串配置值(带默认值)getSubConfig(String prefix) Get sub-configuration by prefix 根据前缀获取子配置booleanCheck if configuration key exists 检查配置键是否存在voidremoveListener(ConfigListener listener) Remove configuration change listener 移除配置变更监听器
-
Method Details
-
getString
Get string configuration value 获取字符串配置值- Parameters:
key- configuration key | 配置键- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found | 如果键未找到
-
getString
-
getInt
Get integer configuration value 获取整数配置值- Parameters:
key- configuration key | 配置键- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found or conversion failed | 如果键未找到或转换失败
-
getInt
Get integer configuration value with default 获取整数配置值(带默认值)- Parameters:
key- configuration key | 配置键defaultValue- default value | 默认值- Returns:
- configuration value or default | 配置值或默认值
-
getLong
Get long configuration value 获取长整数配置值- Parameters:
key- configuration key | 配置键- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found or conversion failed | 如果键未找到或转换失败
-
getLong
Get long configuration value with default 获取长整数配置值(带默认值)- Parameters:
key- configuration key | 配置键defaultValue- default value | 默认值- Returns:
- configuration value or default | 配置值或默认值
-
getDouble
Get double configuration value 获取双精度配置值- Parameters:
key- configuration key | 配置键- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found or conversion failed | 如果键未找到或转换失败
-
getDouble
Get double configuration value with default 获取双精度配置值(带默认值)- Parameters:
key- configuration key | 配置键defaultValue- default value | 默认值- Returns:
- configuration value or default | 配置值或默认值
-
getBoolean
Get boolean configuration value 获取布尔配置值- Parameters:
key- configuration key | 配置键- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found or conversion failed | 如果键未找到或转换失败
-
getBoolean
Get boolean configuration value with default 获取布尔配置值(带默认值)- Parameters:
key- configuration key | 配置键defaultValue- default value | 默认值- Returns:
- configuration value or default | 配置值或默认值
-
getDuration
Get Duration configuration value 获取Duration配置值- Parameters:
key- configuration key | 配置键- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found or conversion failed | 如果键未找到或转换失败
-
getDuration
-
get
Get typed configuration value 获取指定类型配置值- Type Parameters:
T- target type | 目标类型- Parameters:
key- configuration key | 配置键type- target class | 目标类- Returns:
- configuration value | 配置值
- Throws:
OpenConfigException- if key not found or conversion failed | 如果键未找到或转换失败
-
get
Get typed configuration value with default 获取指定类型配置值(带默认值)- Type Parameters:
T- target type | 目标类型- Parameters:
key- configuration key | 配置键type- target class | 目标类defaultValue- default value | 默认值- Returns:
- configuration value or default | 配置值或默认值
-
getList
-
getMap
Get map configuration value 获取映射配置值- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
key- configuration key | 配置键keyType- key class | 键类valueType- value class | 值类- Returns:
- configuration value map | 配置值映射
-
getOptional
-
getOptional
-
getSubConfig
-
getByPrefix
-
hasKey
Check if configuration key exists 检查配置键是否存在- Parameters:
key- configuration key | 配置键- Returns:
- true if exists | 如果存在返回true
-
getKeys
-
addListener
Add configuration change listener 添加配置变更监听器- Parameters:
listener- configuration listener | 配置监听器
-
addListener
Add listener for specific configuration key 添加指定键的监听器- Parameters:
key- configuration key | 配置键listener- configuration listener | 配置监听器
-
removeListener
Remove configuration change listener 移除配置变更监听器- Parameters:
listener- configuration listener | 配置监听器
-
bind
Bind configuration to POJO/Record 绑定配置到POJO/Record- Type Parameters:
T- target type | 目标类型- Parameters:
prefix- configuration prefix | 配置前缀type- target class | 目标类- Returns:
- bound instance | 绑定的实例
- Throws:
OpenConfigException- if binding failed | 如果绑定失败
-
bindTo
Bind configuration to existing object 绑定配置到现有对象- Type Parameters:
T- target type | 目标类型- Parameters:
prefix- configuration prefix | 配置前缀target- target object | 目标对象- Throws:
OpenConfigException- if binding failed | 如果绑定失败
-