Class YamlConfigSource

java.lang.Object
cloud.opencode.base.config.source.YamlConfigSource
All Implemented Interfaces:
ConfigSource

public class YamlConfigSource extends Object implements ConfigSource
YAML Configuration Source YAML配置源

Loads configuration from .yml/.yaml files when opencode-base-yml module is available. Uses reflection to detect yml module presence, allowing graceful degradation.

当opencode-base-yml模块可用时从.yml/.yaml文件加载配置。 使用反射检测yml模块存在性,允许优雅降级。

Features | 主要功能:

  • Optional yml module dependency - yml模块可选依赖
  • Classpath and filesystem support - 支持类路径和文件系统
  • Hot reload for file-based sources - 文件源支持热重载
  • Nested YAML flattening to dot notation - 嵌套YAML扁平化为点号记法

Usage Examples | 使用示例:

// Check if YAML is supported
if (YamlConfigSource.isYamlSupported()) {
    ConfigSource source = new YamlConfigSource("application.yml", true);
}

// From filesystem
ConfigSource source = new YamlConfigSource(Path.of("/etc/app/config.yml"));

YAML Flattening Example | YAML扁平化示例:

# Input YAML:
server:
  port: 8080
  host: localhost
database:
  url: jdbc:mysql://localhost/db

# Flattened to:
server.port=8080
server.host=localhost
database.url=jdbc:mysql://localhost/db

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
    YamlConfigSource(String resource, boolean classpath)
    Create YAML source from classpath or filesystem 从类路径或文件系统创建YAML源
    Create YAML source from filesystem path 从文件系统路径创建YAML源
  • Method Summary

    Modifier and Type
    Method
    Description
    Get configuration source name 获取配置源名称
    int
    Get configuration source priority (higher number = higher priority) 获取配置源优先级(数值越大优先级越高)
    Get all configuration properties 获取所有配置属性
    static boolean
    Check if YAML support is available 检查YAML支持是否可用
    void
    Reload configuration from source 从源重新加载配置
    boolean
    Check if source supports hot reload 检查源是否支持热重载

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface ConfigSource

    getProperty
  • Constructor Details

    • YamlConfigSource

      public YamlConfigSource(String resource, boolean classpath)
      Create YAML source from classpath or filesystem 从类路径或文件系统创建YAML源
      Parameters:
      resource - resource path | 资源路径
      classpath - true for classpath, false for filesystem | 类路径为true,文件系统为false
      Throws:
      OpenConfigException - if YAML module not available | YAML模块不可用时抛出异常
    • YamlConfigSource

      public YamlConfigSource(Path file)
      Create YAML source from filesystem path 从文件系统路径创建YAML源
      Parameters:
      file - file path | 文件路径
      Throws:
      OpenConfigException - if YAML module not available | YAML模块不可用时抛出异常
  • Method Details

    • isYamlSupported

      public static boolean isYamlSupported()
      Check if YAML support is available 检查YAML支持是否可用
      Returns:
      true if opencode-base-yml module is present | yml模块存在返回true
    • getName

      public String getName()
      Description copied from interface: ConfigSource
      Get configuration source name 获取配置源名称
      Specified by:
      getName in interface ConfigSource
      Returns:
      source name | 源名称
    • getProperties

      public Map<String,String> getProperties()
      Description copied from interface: ConfigSource
      Get all configuration properties 获取所有配置属性

      Performance | 性能:

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

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

      Specified by:
      getProperties in interface ConfigSource
      Returns:
      configuration properties map | 配置属性映射
    • getPriority

      public int getPriority()
      Description copied from interface: ConfigSource
      Get configuration source priority (higher number = higher priority) 获取配置源优先级(数值越大优先级越高)

      Default Priorities | 默认优先级:

      • CommandLine: 200
      • Environment: 100
      • Properties/YAML: 50
      • InMemory: 10
      Specified by:
      getPriority in interface ConfigSource
      Returns:
      priority value | 优先级值
    • supportsReload

      public boolean supportsReload()
      Description copied from interface: ConfigSource
      Check if source supports hot reload 检查源是否支持热重载
      Specified by:
      supportsReload in interface ConfigSource
      Returns:
      true if hot reload supported | 如果支持热重载返回true
    • reload

      public void reload()
      Description copied from interface: ConfigSource
      Reload configuration from source 从源重新加载配置

      This method is called when hot reload is enabled.

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

      Specified by:
      reload in interface ConfigSource