Rust dependency injection project, inspired by Spring IOC
.
[dependencies]
autowired="0.1"
Just derive your struct with the marco Component
, you can use the singleton component everywhere.
#[derive(Default, Component)]
struct Bar {
name: String,
age: u32,
}
fn main() {
// create `bar` via Default::default
let bar: Autowired<Bar> = Autowired::new();
assert_eq!(String::default(), bar.name);
assert_eq!(u32::default(), bar.age);
}
Define custom component initialization logic
#[derive(Default)]
struct Foo {
value: String,
}
impl Component for Foo {
type Error = ();
fn new_instance() -> Result<Arc<Self>, Self::Error> {
Ok(Arc::new(Foo {
value: TEST_STRING.to_string(),
}))
}
}
fn main() {
// create `foo` via new_instance
let foo = Autowired::<Foo>::new();
assert_eq!("TEST_STRING", foo.value);
}
By default, components are registered lazily. If you need to register components in advance at the beginning of the program, you can refer to this example:
use autowired::{Component, Bean, setup_submitted_beans};
#[derive(Default, Component, Bean)]
struct Foo;
#[derive(Default, Component, Bean)]
struct Bar;
fn main() {
// register components which derives `Bean`
setup_submitted_beans();
assert!(autowired::exist_component::<Foo>());
assert!(autowired::exist_component::<Bar>());
}