Interface PropertySource

All Known Implementing Classes:
PropertySource.EnvironmentPropertySource, PropertySource.MapPropertySource, PropertySource.SystemPropertySource, PropertySource.YamlPropertySource

public interface PropertySource
Property Source Interface - Abstract Source of Configuration Properties 属性源接口 - 配置属性的抽象来源

Provides a unified interface for accessing configuration properties from various sources (YAML, Maps, environment variables, etc.).

提供统一接口用于从各种来源(YAML、Map、环境变量等) 访问配置属性。

Features | 主要功能:

  • Unified access to YAML, Map, environment, and system properties - 统一访问 YAML、Map、环境变量和系统属性
  • Typed property retrieval with defaults - 带默认值的类型化属性获取
  • Prefix-based property subsetting - 基于前缀的属性子集
  • Optional value support - 可选值支持

Usage Example | 使用示例:

// Create from YAML
PropertySource source = PropertySource.fromYaml(yamlContent);

// Access properties
String host = source.getProperty("database.host");
int port = source.getProperty("database.port", Integer.class, 5432);

// Bind to object
DatabaseConfig config = YmlBinder.bind(source, DatabaseConfig.class);

Security | 安全性:

  • Thread-safe: Depends on implementation (MapPropertySource is immutable) - 线程安全: 取决于实现(MapPropertySource 不可变)
  • Null-safe: Yes (returns null for missing properties) - 空值安全: 是(缺失属性返回 null)
Since:
JDK 25, opencode-base-yml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getName

      String getName()
      Gets the property source name 获取属性源名称
      Returns:
      source name | 源名称
    • getProperty

      String getProperty(String path)
      Gets a property value by path 通过路径获取属性值
      Parameters:
      path - property path | 属性路径
      Returns:
      property value or null | 属性值或 null
    • getProperty

      default String getProperty(String path, String defaultValue)
      Gets a property value with default 获取属性值(带默认值)
      Parameters:
      path - property path | 属性路径
      defaultValue - default value | 默认值
      Returns:
      property value or default | 属性值或默认值
    • getProperty

      <T> T getProperty(String path, Class<T> type)
      Gets a typed property value 获取类型化的属性值
      Type Parameters:
      T - type parameter | 类型参数
      Parameters:
      path - property path | 属性路径
      type - target type | 目标类型
      Returns:
      typed value or null | 类型化值或 null
    • getProperty

      default <T> T getProperty(String path, Class<T> type, T defaultValue)
      Gets a typed property value with default 获取类型化的属性值(带默认值)
      Type Parameters:
      T - type parameter | 类型参数
      Parameters:
      path - property path | 属性路径
      type - target type | 目标类型
      defaultValue - default value | 默认值
      Returns:
      typed value or default | 类型化值或默认值
    • getOptionalProperty

      default Optional<String> getOptionalProperty(String path)
      Gets a property as Optional 获取属性作为 Optional
      Parameters:
      path - property path | 属性路径
      Returns:
      Optional containing value | 包含值的 Optional
    • containsProperty

      boolean containsProperty(String path)
      Checks if a property exists 检查属性是否存在
      Parameters:
      path - property path | 属性路径
      Returns:
      true if exists | 如果存在返回 true
    • getPropertyNames

      Set<String> getPropertyNames()
      Gets all property names 获取所有属性名
      Returns:
      set of property names | 属性名集合
    • getProperties

      Map<String,Object> getProperties()
      Gets properties as Map 获取属性作为 Map
      Returns:
      property map | 属性映射
    • getProperties

      Map<String,Object> getProperties(String prefix)
      Gets properties under a prefix 获取前缀下的属性
      Parameters:
      prefix - the prefix | 前缀
      Returns:
      property map under prefix | 前缀下的属性映射
    • fromMap

      static PropertySource fromMap(String name, Map<String,Object> properties)
      Creates a PropertySource from a Map 从 Map 创建 PropertySource
      Parameters:
      name - source name | 源名称
      properties - property map | 属性映射
      Returns:
      property source | 属性源
    • fromYaml

      static PropertySource fromYaml(String yamlContent)
      Creates a PropertySource from YAML content 从 YAML 内容创建 PropertySource
      Parameters:
      yamlContent - YAML content | YAML 内容
      Returns:
      property source | 属性源
    • fromYaml

      static PropertySource fromYaml(String name, String yamlContent)
      Creates a PropertySource from YAML content with name 从 YAML 内容创建命名的 PropertySource
      Parameters:
      name - source name | 源名称
      yamlContent - YAML content | YAML 内容
      Returns:
      property source | 属性源
    • fromEnvironment

      static PropertySource fromEnvironment()
      Creates a PropertySource from environment variables 从环境变量创建 PropertySource
      Returns:
      property source | 属性源
    • fromSystemProperties

      static PropertySource fromSystemProperties()
      Creates a PropertySource from system properties 从系统属性创建 PropertySource
      Returns:
      property source | 属性源