Class ContextAwareConfig

java.lang.Object
cloud.opencode.base.config.jdk25.ContextAwareConfig
All Implemented Interfaces:
Config

public class ContextAwareConfig extends Object implements Config
Context-Aware Configuration 上下文感知配置

Configuration wrapper that provides context-aware value resolution. Supports tenant isolation and request-scoped configuration overrides using JDK 25 ScopedValue.

提供上下文感知值解析的配置包装器。使用JDK 25 ScopedValue支持租户隔离和请求级配置覆盖。

Features | 主要功能:

  • Tenant-specific configuration - 租户特定配置
  • Request-scoped overrides - 请求级别覆盖
  • Transparent delegation - 透明委托
  • ScopedValue integration - ScopedValue集成

Usage Examples | 使用示例:

Config baseConfig = OpenConfig.load();
ContextAwareConfig config = new ContextAwareConfig(baseConfig);

// Multi-tenant access
ConfigContext.withTenant("tenant-1", () -> {
    // Gets tenants.tenant-1.db.url or falls back to db.url
    String dbUrl = config.getString("db.url");
});

// Request-scoped overrides
ConfigContext.withOverrides(Map.of("log.level", "DEBUG"), () -> {
    String level = config.getString("log.level"); // -> "DEBUG"
});

Resolution Order | 解析顺序:

  1. Request-scoped overrides - 请求级覆盖
  2. Tenant-specific configuration - 租户特定配置
  3. Base configuration - 基础配置

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-config V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Add configuration change listener 添加配置变更监听器
    void
    Add listener for specific configuration key 添加指定键的监听器
    <T> T
    bind(String prefix, Class<T> type)
    Bind configuration to POJO/Record 绑定配置到POJO/Record
    <T> void
    bindTo(String prefix, T target)
    Bind configuration to existing object 绑定配置到现有对象
    <T> T
    get(String key, Class<T> type)
    Get typed configuration value 获取指定类型配置值
    <T> T
    get(String key, Class<T> type, T defaultValue)
    Get typed configuration value with default 获取指定类型配置值(带默认值)
    boolean
    Get boolean configuration value 获取布尔配置值
    boolean
    getBoolean(String key, boolean defaultValue)
    Get boolean configuration value with default 获取布尔配置值(带默认值)
    Get all configurations with specified prefix 获取指定前缀的所有配置
    double
    Get double configuration value 获取双精度配置值
    double
    getDouble(String key, double defaultValue)
    Get double configuration value with default 获取双精度配置值(带默认值)
    Get Duration configuration value 获取Duration配置值
    getDuration(String key, Duration defaultValue)
    Get Duration configuration value with default 获取Duration配置值(带默认值)
    int
    Get integer configuration value 获取整数配置值
    int
    getInt(String key, int defaultValue)
    Get integer configuration value with default 获取整数配置值(带默认值)
    Get all configuration keys 获取所有配置键
    <T> List<T>
    getList(String key, Class<T> elementType)
    Get list configuration value 获取列表配置值
    long
    Get long configuration value 获取长整数配置值
    long
    getLong(String key, long defaultValue)
    Get long configuration value with default 获取长整数配置值(带默认值)
    <K,V> Map<K,V>
    getMap(String key, Class<K> keyType, Class<V> valueType)
    Get map configuration value 获取映射配置值
    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 获取字符串配置值
    getString(String key, String defaultValue)
    Get string configuration value with default 获取字符串配置值(带默认值)
    Get sub-configuration by prefix 根据前缀获取子配置
    boolean
    Check if configuration key exists 检查配置键是否存在
    void
    Remove configuration change listener 移除配置变更监听器

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ContextAwareConfig

      public ContextAwareConfig(Config delegate)
  • Method Details

    • getString

      public String getString(String key)
      Description copied from interface: Config
      Get string configuration value 获取字符串配置值
      Specified by:
      getString in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
    • getString

      public String getString(String key, String defaultValue)
      Description copied from interface: Config
      Get string configuration value with default 获取字符串配置值(带默认值)
      Specified by:
      getString in interface Config
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value if key not found | 键未找到时的默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getInt

      public int getInt(String key)
      Description copied from interface: Config
      Get integer configuration value 获取整数配置值
      Specified by:
      getInt in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
    • getInt

      public int getInt(String key, int defaultValue)
      Description copied from interface: Config
      Get integer configuration value with default 获取整数配置值(带默认值)
      Specified by:
      getInt in interface Config
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getLong

      public long getLong(String key)
      Description copied from interface: Config
      Get long configuration value 获取长整数配置值
      Specified by:
      getLong in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
    • getLong

      public long getLong(String key, long defaultValue)
      Description copied from interface: Config
      Get long configuration value with default 获取长整数配置值(带默认值)
      Specified by:
      getLong in interface Config
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getDouble

      public double getDouble(String key)
      Description copied from interface: Config
      Get double configuration value 获取双精度配置值
      Specified by:
      getDouble in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
    • getDouble

      public double getDouble(String key, double defaultValue)
      Description copied from interface: Config
      Get double configuration value with default 获取双精度配置值(带默认值)
      Specified by:
      getDouble in interface Config
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getBoolean

      public boolean getBoolean(String key)
      Description copied from interface: Config
      Get boolean configuration value 获取布尔配置值
      Specified by:
      getBoolean in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
    • getBoolean

      public boolean getBoolean(String key, boolean defaultValue)
      Description copied from interface: Config
      Get boolean configuration value with default 获取布尔配置值(带默认值)
      Specified by:
      getBoolean in interface Config
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getDuration

      public Duration getDuration(String key)
      Description copied from interface: Config
      Get Duration configuration value 获取Duration配置值
      Specified by:
      getDuration in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
    • getDuration

      public Duration getDuration(String key, Duration defaultValue)
      Description copied from interface: Config
      Get Duration configuration value with default 获取Duration配置值(带默认值)
      Specified by:
      getDuration in interface Config
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • get

      public <T> T get(String key, Class<T> type)
      Description copied from interface: Config
      Get typed configuration value 获取指定类型配置值
      Specified by:
      get in interface Config
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      key - configuration key | 配置键
      type - target class | 目标类
      Returns:
      configuration value | 配置值
    • get

      public <T> T get(String key, Class<T> type, T defaultValue)
      Description copied from interface: Config
      Get typed configuration value with default 获取指定类型配置值(带默认值)
      Specified by:
      get in interface Config
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      key - configuration key | 配置键
      type - target class | 目标类
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getList

      public <T> List<T> getList(String key, Class<T> elementType)
      Description copied from interface: Config
      Get list configuration value 获取列表配置值
      Specified by:
      getList in interface Config
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      key - configuration key | 配置键
      elementType - element class | 元素类
      Returns:
      configuration value list | 配置值列表
    • getMap

      public <K,V> Map<K,V> getMap(String key, Class<K> keyType, Class<V> valueType)
      Description copied from interface: Config
      Get map configuration value 获取映射配置值
      Specified by:
      getMap in interface Config
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      key - configuration key | 配置键
      keyType - key class | 键类
      valueType - value class | 值类
      Returns:
      configuration value map | 配置值映射
    • getOptional

      public Optional<String> getOptional(String key)
      Description copied from interface: Config
      Get optional string configuration value 获取Optional字符串配置值
      Specified by:
      getOptional in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      optional configuration value | Optional配置值
    • getOptional

      public <T> Optional<T> getOptional(String key, Class<T> type)
      Description copied from interface: Config
      Get optional typed configuration value 获取Optional类型配置值
      Specified by:
      getOptional in interface Config
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      key - configuration key | 配置键
      type - target class | 目标类
      Returns:
      optional configuration value | Optional配置值
    • getSubConfig

      public Config getSubConfig(String prefix)
      Description copied from interface: Config
      Get sub-configuration by prefix 根据前缀获取子配置
      Specified by:
      getSubConfig in interface Config
      Parameters:
      prefix - configuration prefix | 配置前缀
      Returns:
      sub-configuration | 子配置
    • getByPrefix

      public Map<String,String> getByPrefix(String prefix)
      Description copied from interface: Config
      Get all configurations with specified prefix 获取指定前缀的所有配置
      Specified by:
      getByPrefix in interface Config
      Parameters:
      prefix - configuration prefix | 配置前缀
      Returns:
      configuration map | 配置映射
    • hasKey

      public boolean hasKey(String key)
      Description copied from interface: Config
      Check if configuration key exists 检查配置键是否存在
      Specified by:
      hasKey in interface Config
      Parameters:
      key - configuration key | 配置键
      Returns:
      true if exists | 如果存在返回true
    • getKeys

      public Set<String> getKeys()
      Description copied from interface: Config
      Get all configuration keys 获取所有配置键
      Specified by:
      getKeys in interface Config
      Returns:
      set of all keys | 所有键的集合
    • addListener

      public void addListener(ConfigListener listener)
      Description copied from interface: Config
      Add configuration change listener 添加配置变更监听器
      Specified by:
      addListener in interface Config
      Parameters:
      listener - configuration listener | 配置监听器
    • addListener

      public void addListener(String key, ConfigListener listener)
      Description copied from interface: Config
      Add listener for specific configuration key 添加指定键的监听器
      Specified by:
      addListener in interface Config
      Parameters:
      key - configuration key | 配置键
      listener - configuration listener | 配置监听器
    • removeListener

      public void removeListener(ConfigListener listener)
      Description copied from interface: Config
      Remove configuration change listener 移除配置变更监听器
      Specified by:
      removeListener in interface Config
      Parameters:
      listener - configuration listener | 配置监听器
    • bind

      public <T> T bind(String prefix, Class<T> type)
      Description copied from interface: Config
      Bind configuration to POJO/Record 绑定配置到POJO/Record
      Specified by:
      bind in interface Config
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      prefix - configuration prefix | 配置前缀
      type - target class | 目标类
      Returns:
      bound instance | 绑定的实例
    • bindTo

      public <T> void bindTo(String prefix, T target)
      Description copied from interface: Config
      Bind configuration to existing object 绑定配置到现有对象
      Specified by:
      bindTo in interface Config
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      prefix - configuration prefix | 配置前缀
      target - target object | 目标对象