Dependency inject
spring-rs provides a simple implementation of compile-time dependency injection
Compile-time dependency inject
spring-rs provides a special Component - Service, which supports injecting dependent components at compile time.
Like the following example, UserService
only needs to derive the Service
trait. In order to distinguish the injected dependencies, you need to specify whether the dependency is a Component or a Config through the attribute macros #[component]
and #[config]
.
use spring_sqlx::ConnectPool;
use spring::config::Configurable;
use spring::plugin::service::Service;
use serde::Deserialize;
#[derive(Clone, Configurable, Deserialize)]
#[config_prefix = "user"]
struct UserConfig {
username: String,
project: String,
}
#[derive(Clone, Service)]
struct UserService {
#[component]
db: ConnectPool,
#[config]
config: UserConfig,
}
For the complete code, see dependency-inject-example
.