pub struct Config { /* private fields */ }Expand description
Main configuration structure 主配置结构
Equivalent to Spring Boot’s ConfigurableEnvironment and ConfigFileApplicationListener.
等价于Spring Boot的ConfigFileApplicationListener和ConfigurableEnvironment。
§Example / 示例
use hiver_config::Config;
let config = Config::builder()
.add_file("config/application.yaml")
.add_profile("dev")
.build()?;Implementations§
Source§impl Config
impl Config
Sourcepub fn builder() -> ConfigBuilder
pub fn builder() -> ConfigBuilder
Create a configuration builder 创建配置构建器
Sourcepub fn load() -> ConfigResult<Self>
pub fn load() -> ConfigResult<Self>
Load configuration with default settings 使用默认设置加载配置
Default loading order / 默认加载顺序:
- application.properties (or .yaml, .toml, .json)
- application-{profile}.properties
- System environment variables
- Command line arguments
Sourcepub fn from_file<P: AsRef<Path>>(path: P) -> ConfigResult<Self>
pub fn from_file<P: AsRef<Path>>(path: P) -> ConfigResult<Self>
Load configuration from a specific file 从特定文件加载配置
Sourcepub fn add_property_source(&self, source: PropertySource)
pub fn add_property_source(&self, source: PropertySource)
Add a property source 添加属性源
Sourcepub fn add_property_source_first(&self, source: PropertySource)
pub fn add_property_source_first(&self, source: PropertySource)
Add a property source with highest priority. 添加最高优先级的属性源。
Sourcepub fn get_as<T>(&self, key: &str) -> ConfigResult<T>where
T: DeserializeOwned,
pub fn get_as<T>(&self, key: &str) -> ConfigResult<T>where
T: DeserializeOwned,
Get a property as a specific type 获取特定类型的属性
Sourcepub fn get_required(&self, key: &str) -> ConfigResult<Value>
pub fn get_required(&self, key: &str) -> ConfigResult<Value>
Get a required property 获取必需属性
Sourcepub fn get_required_as<T>(&self, key: &str) -> ConfigResult<T>where
T: DeserializeOwned,
pub fn get_required_as<T>(&self, key: &str) -> ConfigResult<T>where
T: DeserializeOwned,
Get a required property as a specific type 获取特定类型的必需属性
Sourcepub fn get_or<T>(&self, key: &str, default: T) -> Twhere
T: DeserializeOwned,
pub fn get_or<T>(&self, key: &str, default: T) -> Twhere
T: DeserializeOwned,
Get property with default value 获取带默认值的属性
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Check if a property exists 检查属性是否存在
Sourcepub fn get_prefix(&self, prefix: &str) -> IndexMap<String, Value>
pub fn get_prefix(&self, prefix: &str) -> IndexMap<String, Value>
Get all properties starting with a prefix 获取所有以指定前缀开头的属性
Sourcepub fn environment(&self) -> &Environment
pub fn environment(&self) -> &Environment
Get environment reference 获取环境引用
Sourcepub fn reload_strategy(&self) -> ReloadStrategy
pub fn reload_strategy(&self) -> ReloadStrategy
Get reload strategy 获取重新加载策略
Sourcepub fn reload(&self) -> ConfigResult<()>
pub fn reload(&self) -> ConfigResult<()>
Reload configuration 重新加载配置