Skip to main content

Components

An async dependency-injection container. Components are registered as blueprints (a recipe for constructing T) and can be pulled out of Axum handlers with the Inject extractor.

Core types​

TypePurpose
ComponentProviderRegistration-time container. Register blueprints, then call .build().
ComponentsImmutable, Clone-cheap snapshot of every built component — this should be axum app state.
Inject<T>FromRequestParts<Components> extractor; derefs to &T.
use axumstart_components::{ComponentProvider, Component};

#[derive(Component)] // automatically registers blueprint
struct Settings {
#[default]
port: u16,
}

let provider = ComponentProvider::default();
provider.register::<Settings>();

let components = provider.build().await; // resolves every registered blueprint
let settings = components.get::<Settings>().unwrap();

Registering a component​

Three ways to define one:

  • Struct components — #[derive(Component)] (most common) and trait-object registration via #[as_trait(Trait)].
  • Function components — #[component] on a free function, for construction logic that doesn't fit a plain struct literal.
  • Config components — #[derive(ComponentConfig)] for env var configuration