Multibinder.RealMultibinder
public abstract class Multibinder<T>
extends java.lang.Object
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:
class SnackMachine {
@Inject
public SnackMachine(Set<Snack> snacks) { ... }
}
Create multibindings from different modules is supported. For example, it
is okay to have both CandyModule
and ChipsModule
to both
create their own Multibinder<Snack>
, and to each contribute bindings
to the set of snacks. When that set is injected, it will contain elements
from both modules.
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 be used to create different sets of the same element type. Each distinct annotation gets its own independent collection of elements.
Elements must be distinct. If multiple bound elements have the same value, set injection will fail.
Elements must be non-null. If any set element is null, set injection will fail.
Modifier and Type | Class | Description |
---|---|---|
static class |
Multibinder.RealMultibinder<T> |
The actual multibinder plays several roles:
|
Modifier and Type | Method | Description |
---|---|---|
abstract LinkedBindingBuilder<T> |
addBinding() |
Returns a binding builder used to add a new element in the set.
|
static <T> Multibinder<T> |
newSetBinder(Binder binder,
java.lang.Class<T> type) |
Returns a new multibinder that collects instances of
type in a Set that is
itself bound with no binding annotation. |
static <T> Multibinder<T> |
newSetBinder(Binder binder,
java.lang.Class<T> type,
java.lang.annotation.Annotation annotation) |
Returns a new multibinder that collects instances of
type in a Set that is
itself bound with annotation . |
static <T> Multibinder<T> |
newSetBinder(Binder binder,
java.lang.Class<T> type,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) |
Returns a new multibinder that collects instances of
type in a Set that is
itself bound with annotationType . |
static <T> Multibinder<T> |
newSetBinder(Binder binder,
TypeLiteral<T> type) |
Returns a new multibinder that collects instances of
type in a Set that is
itself bound with no binding annotation. |
static <T> Multibinder<T> |
newSetBinder(Binder binder,
TypeLiteral<T> type,
java.lang.annotation.Annotation annotation) |
Returns a new multibinder that collects instances of
type in a Set that is
itself bound with annotation . |
static <T> Multibinder<T> |
newSetBinder(Binder binder,
TypeLiteral<T> type,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) |
Returns a new multibinder that collects instances of
type in a Set that is
itself bound with annotationType . |
public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type)
type
in a Set
that is
itself bound with no binding annotation.public static <T> Multibinder<T> newSetBinder(Binder binder, java.lang.Class<T> type)
type
in a Set
that is
itself bound with no binding annotation.public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type, java.lang.annotation.Annotation annotation)
type
in a Set
that is
itself bound with annotation
.public static <T> Multibinder<T> newSetBinder(Binder binder, java.lang.Class<T> type, java.lang.annotation.Annotation annotation)
type
in a Set
that is
itself bound with annotation
.public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
type
in a Set
that is
itself bound with annotationType
.public static <T> Multibinder<T> newSetBinder(Binder binder, java.lang.Class<T> type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
type
in a Set
that is
itself bound with annotationType
.public abstract LinkedBindingBuilder<T> addBinding()
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.