7个版本
| 新 0.0.8 | 2024年8月25日 | 
|---|---|
| 0.0.7 | 2024年8月21日 | 
#166 在 配置 中排名
每月 622 次下载
在  8 crates 中使用
31KB
681 代码行
简介
spring-boot 是 spring 项目的核心模块,包括:配置管理、插件管理和组件管理。
所有插件都需要实现 Plugin 特性。
如何编写自己的插件
添加依赖
spring-boot = { version = "0.0.8" }      # This crate contains the definition of plugin traits
serde = { workspace = true, features = ["derive"] } # Used to parse plugin configuration items
struct MyPlugin;
#[async_trait]
impl Plugin for MyPlugin {
    async fn build(&self, app: &mut AppBuilder) {
        // Call app.get_config::<Config>(self) method to get configuration items
        match app.get_config::<Config>(self) {
            Ok(config) => {
                println!("{:#?}", config);
                assert_eq!(config.a, 1);
                assert_eq!(config.b, true);
                // Get the configuration items to build the corresponding components
            }
            Err(e) => println!("{:?}", e),
        }
    }
}
/// Configuration item prefix
impl Configurable for MyPlugin {
    fn config_prefix(&self) -> &str {
        "my-plugin"
    }
}
/// Plugin configuration
#[derive(Debug, Deserialize)]
struct Config {
    a: u32,
    b: bool,
}
您可以使用 derive 宏来实现 Configurable 特性
/// Use the `config_prefix` attr macro to define the prefix configured in the toml file
#[derive(Configurable)]
#[config_prefix = "my-plugin"]
struct MyPlugin;
完整代码请参阅 plugin-example,或参考其他内置插件代码。
依赖项
~10–18MB
~226K SLoC