Skip to content

Latest commit

 

History

History
89 lines (72 loc) · 2.61 KB

File metadata and controls

89 lines (72 loc) · 2.61 KB
  1. summary Configuring injections

By default, no configuration is needed. For example:

If you need advanced configuration, then you need to use `Injector`. Other frameworks also call it a container, and usually, it provides their core functionality. In `inject` it is absolutely optional.

Create an injector, and *register* it with `inject.register(injector)`. You need to register it, so that the injections get to know about it, and actually use it. Only one injector can be registered at the same time. You can unregister an injector any time with `inject.unregister`. Without any arguments the function unregisters any registered injector, with a given injector, it unregisters the injector only if it is registered.

Use `injector.bind` to attach providers to components. It takes almost the same arguments as injections, except for not having `attr`/`param` and using `to` instead of `bindto`:

See [Injections#bindto] for more information about binding. Simple example:

There is no difference whether you first create bindings, then register an injector or vice versa.

It is possible to get instances directly from an injector with `get_instance(type, annotation=None)`, but it uses only injector's bindings.

If you want to imitate Guice's configuration modules, you can use simple functions (or whatever).