Interface ConfigSource

All Known Implementing Classes:
CommandLineConfigSource, CompositeConfigSource, EnvironmentConfigSource, InMemoryConfigSource, PropertiesConfigSource, SystemPropertiesConfigSource, YamlConfigSource

public interface ConfigSource
Configuration Source Interface 配置源接口

Represents a source of configuration properties. Implementations can load configuration from various sources like files, environment variables, system properties, databases, etc.

表示配置属性的来源。实现类可以从文件、环境变量、系统属性、数据库等各种源加载配置。

Features | 主要功能:

  • Configuration source abstraction - 配置源抽象
  • Priority-based source ordering - 基于优先级的源排序
  • Hot reload support - 热重载支持
  • SPI extensibility - SPI扩展性

Usage Examples | 使用示例:

// Implement custom source
public class DatabaseConfigSource implements ConfigSource {
    @Override
    public String getName() {
        return "database";
    }

    @Override
    public Map<String, String> getProperties() {
        return loadFromDatabase();
    }

    @Override
    public int getPriority() {
        return 75; // Between properties and environment
    }
}

// Use in builder
Config config = OpenConfig.builder()
    .addSource(new DatabaseConfigSource())
    .build();

Performance | 性能特性:

  • getProperties() should be cached - getProperties()应该被缓存
  • Reload should check modification time - 重载应检查修改时间

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Immutable properties map recommended - 推荐使用不可变属性映射
Since:
JDK 25, opencode-base-config V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Get configuration source name 获取配置源名称
    default int
    Get configuration source priority (higher number = higher priority) 获取配置源优先级(数值越大优先级越高)
    Get all configuration properties 获取所有配置属性
    default String
    Get configuration value by key 根据键获取配置值
    default void
    Reload configuration from source 从源重新加载配置
    default boolean
    Check if source supports hot reload 检查源是否支持热重载
  • Method Details

    • getName

      String getName()
      Get configuration source name 获取配置源名称
      Returns:
      source name | 源名称
    • getProperties

      Map<String,String> getProperties()
      Get all configuration properties 获取所有配置属性

      Performance | 性能:

      This method should return a cached immutable map for best performance.

      此方法应返回缓存的不可变映射以获得最佳性能。

      Returns:
      configuration properties map | 配置属性映射
    • getProperty

      default String getProperty(String key)
      Get configuration value by key 根据键获取配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value or null | 配置值或null
    • getPriority

      default int getPriority()
      Get configuration source priority (higher number = higher priority) 获取配置源优先级(数值越大优先级越高)

      Default Priorities | 默认优先级:

      • CommandLine: 200
      • Environment: 100
      • Properties/YAML: 50
      • InMemory: 10
      Returns:
      priority value | 优先级值
    • supportsReload

      default boolean supportsReload()
      Check if source supports hot reload 检查源是否支持热重载
      Returns:
      true if hot reload supported | 如果支持热重载返回true
    • reload

      default void reload()
      Reload configuration from source 从源重新加载配置

      This method is called when hot reload is enabled.

      启用热重载时会调用此方法。