Class Multibinder<T>
- java.lang.Object
-
- com.google.inject.multibindings.Multibinder<T>
-
public class Multibinder<T> extends Object
An API to bind multiple values separately, only to later inject them as a complete collection. Multibinder is intended for use in your application's module:public class SnacksModule extends AbstractModule { protected void configure() { Multibinder<Snack> multibinder = Multibinder.newSetBinder(binder(), Snack.class); multibinder.addBinding().toInstance(new Twix()); multibinder.addBinding().toProvider(SnickersProvider.class); multibinder.addBinding().to(Skittles.class); } }
With this binding, a
Set
<Snack>
can now be injected:
If desired,class SnackMachine { @Inject public SnackMachine(Set<Snack> snacks) { ... } }
Collection
<Provider<Snack>>
can also be injected.Contributing multibindings from different modules is supported. For example, it is okay for both
CandyModule
andChipsModule
to create their ownMultibinder<Snack>
, and to each contribute bindings to the set of snacks. When that set is injected, it will contain elements from both modules.The set's iteration order is consistent with the binding order. This is convenient when multiple elements are contributed by the same module because that module can order its bindings appropriately. Avoid relying on the iteration order of elements contributed by different modules, since there is no equivalent mechanism to order modules.
The set is unmodifiable. Elements can only be added to the set by configuring the multibinder. Elements can never be removed from the set.
Elements are resolved at set injection time. If an element is bound to a provider, that provider's get method will be called each time the set is injected (unless the binding is also scoped).
Annotations are used to create different sets of the same element type. Each distinct annotation gets its own independent collection of elements.
Elements must be distinct unless
permitDuplicates()
is specified. WithoutpermitDuplicates()
, set injection will fail if multiple bound elements have the same value.Elements must be non-null. If any set element is null, set injection will fail.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description LinkedBindingBuilder<T>
addBinding()
Returns a binding builder used to add a new element in the set.boolean
equals(Object obj)
int
hashCode()
static <T> Multibinder<T>
newSetBinder(Binder binder, Key<T> key)
Returns a new multibinder that collects instances of the key's type in aSet
that is itself bound with the annotation (if any) of the key.static <T> Multibinder<T>
newSetBinder(Binder binder, TypeLiteral<T> type)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound with no binding annotation.static <T> Multibinder<T>
newSetBinder(Binder binder, TypeLiteral<T> type, Annotation annotation)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotation
.static <T> Multibinder<T>
newSetBinder(Binder binder, TypeLiteral<T> type, Class<? extends Annotation> annotationType)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotationType
.static <T> Multibinder<T>
newSetBinder(Binder binder, Class<T> type)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound with no binding annotation.static <T> Multibinder<T>
newSetBinder(Binder binder, Class<T> type, Annotation annotation)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotation
.static <T> Multibinder<T>
newSetBinder(Binder binder, Class<T> type, Class<? extends Annotation> annotationType)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotationType
.Multibinder<T>
permitDuplicates()
Configures the bound set to silently discard duplicate elements.
-
-
-
Method Detail
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound with no binding annotation.
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound with no binding annotation.
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type, Annotation annotation)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotation
.
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type, Annotation annotation)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotation
.
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type, Class<? extends Annotation> annotationType)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotationType
.
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, Key<T> key)
Returns a new multibinder that collects instances of the key's type in aSet
that is itself bound with the annotation (if any) of the key.- Since:
- 4.0
-
newSetBinder
public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type, Class<? extends Annotation> annotationType)
Returns a new multibinder that collects instances oftype
in aSet
that is itself bound withannotationType
.
-
permitDuplicates
public Multibinder<T> permitDuplicates()
Configures the bound set to silently discard duplicate elements. When multiple equal values are bound, the one that gets included is arbitrary. When multiple modules contribute elements to the set, this configuration option impacts all of them.- Returns:
- this multibinder
- Since:
- 3.0
-
addBinding
public LinkedBindingBuilder<T> addBinding()
Returns a binding builder used to add a new element in the set. Each bound element must have a distinct value. Bound providers will be evaluated each time the set is injected.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.
-
-