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:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classEnvironment variable PropertySource 环境变量 PropertySourcestatic classMap-based PropertySource implementation 基于 Map 的 PropertySource 实现static classSystem properties PropertySource 系统属性 PropertySourcestatic classYAML-based PropertySource implementation 基于 YAML 的 PropertySource 实现 -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsProperty(String path) Checks if a property exists 检查属性是否存在static PropertySourceCreates a PropertySource from environment variables 从环境变量创建 PropertySourcestatic PropertySourceCreates a PropertySource from a Map 从 Map 创建 PropertySourcestatic PropertySourceCreates a PropertySource from system properties 从系统属性创建 PropertySourcestatic PropertySourceCreates a PropertySource from YAML content 从 YAML 内容创建 PropertySourcestatic PropertySourceCreates a PropertySource from YAML content with name 从 YAML 内容创建命名的 PropertySourcegetName()Gets the property source name 获取属性源名称getOptionalProperty(String path) Gets a property as Optional 获取属性作为 OptionalGets properties as Map 获取属性作为 MapgetProperties(String prefix) Gets properties under a prefix 获取前缀下的属性getProperty(String path) Gets a property value by path 通过路径获取属性值<T> TgetProperty(String path, Class<T> type) Gets a typed property value 获取类型化的属性值default <T> TgetProperty(String path, Class<T> type, T defaultValue) Gets a typed property value with default 获取类型化的属性值(带默认值)default StringgetProperty(String path, String defaultValue) Gets a property value with default 获取属性值(带默认值)Gets all property names 获取所有属性名
-
Method Details
-
getName
-
getProperty
-
getProperty
-
getProperty
-
getProperty
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
-
containsProperty
Checks if a property exists 检查属性是否存在- Parameters:
path- property path | 属性路径- Returns:
- true if exists | 如果存在返回 true
-
getPropertyNames
-
getProperties
-
getProperties
-
fromMap
Creates a PropertySource from a Map 从 Map 创建 PropertySource- Parameters:
name- source name | 源名称properties- property map | 属性映射- Returns:
- property source | 属性源
-
fromYaml
Creates a PropertySource from YAML content 从 YAML 内容创建 PropertySource- Parameters:
yamlContent- YAML content | YAML 内容- Returns:
- property source | 属性源
-
fromYaml
Creates a PropertySource from YAML content with name 从 YAML 内容创建命名的 PropertySource- Parameters:
name- source name | 源名称yamlContent- YAML content | YAML 内容- Returns:
- property source | 属性源
-
fromEnvironment
Creates a PropertySource from environment variables 从环境变量创建 PropertySource- Returns:
- property source | 属性源
-
fromSystemProperties
Creates a PropertySource from system properties 从系统属性创建 PropertySource- Returns:
- property source | 属性源
-