- Type Parameters:
T
- the type of the class specified in this multiBinder
public interface MultiBinder<T>
Module snacksModule = binder -> {
MultiBinder<Snack> multiBinder = binder.multiBinder(Snack.class);
multiBinder.addBinding().toInstance(new Twix());
multiBinder.addBinding().toProvider(SnickersProvider.class);
multiBinder.addBinding().to(Skittles.class);
};
With this binding, a List
<Snack>
can now be injected, as:
class SnackMachine {
@Inject
public SnackMachine(List<Snack> snacks) { ... }
}
If desired, Collection
<Provider<Snack>>
can also be injected.
Contributing multiBindings from different modules is supported. For example, it is okay for both
CandyModule
and ChipsModule
to create their own MultiBinder<Snack>
, and to each contribute
bindings to the list of snacks. When that list is injected, it will contain elements from both modules.
The injected list is unmodifiable and elements can only be added to the list by configuring the multiBinder. Elements can not be removed from the list.
Annotations can be used to create different lists of the same element type. Each distinct annotation gets its own independent collection of elements.
-
Method Summary
Modifier and Type Method Description LinkedBindingBuilder<T>
addBinding()
Returns theLinkedBindingBuilder
used to add a new element into the list.
-
Method Details
-
addBinding
LinkedBindingBuilder<T> addBinding()Returns theLinkedBindingBuilder
used to add a new element into the list.It is an error to call this method without also calling one of the
to
methods on the returned binding builder.Scoping elements independently is supported. Use the
in
method to specify a binding scope.- Returns:
- the linked binding builder
-