#[singleton]Expand description
Declares the annotated item as a singleton provider.
The instance of a singleton provider is constructed only once and shared by
every resolution, so it can be resolved by reference or, when the type
implements Clone, by value. The annotated type also gets an implementation
of next_web_core::traits::singleton::Singleton returning the name of the
provider.
The macro can be applied to a struct, an enum, an impl block or a
fn:
- on a
struct, anenumor animplblock the annotated item is the provided type, - on a
fnastructnamed after the function is created and the provided type is the return type of the function. The struct carries no value, it only gives the provider a type of its own.
§Arguments
name = "...": name of the provider. On afnit defaults to the name of the function in camel case, sofn test_nameprovidestestName; on astructit defaults to the type name with its first character lower cased, e.g.MyServicebecomesmyService.eager_create: construct the instance while the provider is registered instead of while it is resolved for the first time.condition = pathorcondition = |cx| ...: a function or a closure taking&dyn ApplicationContextthat decides whether the provider is inserted into the context.binds = [path, ...]: expressions converting the instance into the types it is bound to, e.g.binds = [Self::into_service].async: whether the constructor of astructor anenumis async. Not supported on afn, which is async when it is declared asasync fn.auto_register: whether the provider is submitted to the auto register registry,trueby default. Generic items have to disable it withauto_register = false.default: build the instance withDefault::default()instead of resolving the fields of the annotatedstruct.
§Field and argument attributes
#[autowired(name = "...")]: name of the provider to resolve, which wins over the defaults below. Without it the name of the field or of the argument is used, somy_serviceresolvesmyService; a field or an argument without a name of its own uses the name of its type with its first character lower cased, so&MyServiceresolvesmyService.#[autowired(option)]: the field is anOption<T>that isNonewhen there is no provider with the given name.#[autowired(default)]: the field usesDefault::default()when there is no provider with the given name;#[autowired(default = expr)]uses the given expression instead.#[autowired(vec)]: the field is aVec<T>collecting every provider ofT.#[autowired(map)]: the field is aHashMap<String, T>naming every provider ofT;Thas to implementSingleton.#[autowired(ref)]: the field takes a reference to the provider, which is required when a type alias hides the reference of the field type.- A field or an argument declared as a reference, e.g.
&MyServiceor&mut MyService, is taken from the context as a reference to the instance and does not need#[autowired(ref)]; the instance is created in the context first when it does not exist yet. The context lends any number of immutable references, or a single mutable one, so a&mutfield or argument cannot be combined with another reference. #[value(key = "...")]: the field takes the value of the given application property.#[resource(path = ...)]: path of the crate used by the generated code,::next_web_contextby default.
§Example
ⓘ
#[singleton(binds = [Self::into_service])]
#[derive(Clone)]
struct MyService {
#[autowired(name = "myRepository")]
repository: Arc<dyn Repository>,
}