Skip to the content.

Fill Primer

Definitions

Injection Target

An injection target is either a

Injection targets are identified by “injection annotations,” and have a type and a list of annotations.

Injection Annotations

An injection annotation is an annotation annotated with @InjectionAnnotation,

Matchers

A matcher is a Predicate<InjectionTarget>. There are some simple matcher implementations in the Matchers interface, which match InjectionTargets based on their type and/or annotations.

Binding

A binding provides a mapping from InjectionTargets to InjectionResults, which contain the value that should actually be injecting. Bindings can also claim or reject InjectionTargets to decide which ones they want to handle.

Injector

An injector takes a binding and uses it to actually carry out the injections.


Usage

Unless you need to do something particularly fancy, most injection can be carried out using Injector.builder(). For example, an injector which binds all injectable ints to the value 42 would look like this:

ReflectionInjector.builder()
        .bind(int.class).toInstance(42)
        .build();

The builder also supports using generics from the Jype library, so an injector which binds all injectable List<String>s to {"Hello", "World"} might look like this:

ReflectionInjector.builder()
        .bind(new JTypeToken<List<String>>() {}).toInstance(List.of("Hello", "World"))
        .build();

You can also bind to a type and a target annotation, so an injector which binds ints annotated with @MeaningOfLife might look like this:

ReflectionInjector.builder()
        .bind(int.class, MeaningOfLife.class).toInstance(42)
        .build();

Finally, the built Injector provides a few different methods to carry out injection:


Home | Contact