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) { ... }
}
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 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.
Without permitDuplicates(), 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.
| Modifier and Type | Field and Description |
|---|---|
| private final RealMultibinder |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public LinkedBindingBuilder | |
| public boolean | equals(Object
the reference object with which to compare. obj)Overrides java. |
| public int | |
| public static <T> Multibinder | newSetBinder(Binder binder, TypeLiteral<T> type)
Returns a new multibinder that collects instances of |
| public static <T> Multibinder | newSetBinder(Binder binder, Class<T> type)
Returns a new multibinder that collects instances of |
| public static <T> Multibinder | newSetBinder(Binder binder, TypeLiteral<T> type, Annotation annotation)
Returns a new multibinder that collects instances of |
| public static <T> Multibinder | newSetBinder(Binder binder, Class<T> type, Annotation annotation)
Returns a new multibinder that collects instances of |
| public static <T> Multibinder | newSetBinder(Binder binder, TypeLiteral<T> type, Class<? extends Annotation> annotationType)
Returns a new multibinder that collects instances of |
| public static <T> Multibinder | newSetBinder(Binder binder, Key<T> key)
Returns a new multibinder that collects instances of the key's type in a |
| public static <T> Multibinder | newSetBinder(Binder binder, Class<T> type, Class<? extends Annotation> annotationType)
Returns a new multibinder that collects instances of |
| public Multibinder | Returns: this multibinderConfigures the bound set to silently discard duplicate elements. |
| delegate | back to summary |
|---|---|
| private final RealMultibinder<T> delegate | |
| Multibinder | back to summary |
|---|---|
| private Multibinder(RealMultibinder<T> delegate) | |
| addBinding | back to summary |
|---|---|
| public LinkedBindingBuilder 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 Scoping elements independently is supported. Use the | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of | |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of | |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of | |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of | |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of | |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of the key's type in a
| |
| newSetBinder | back to summary |
|---|---|
| public static <T> Multibinder Returns a new multibinder that collects instances of | |
| permitDuplicates | back to summary |
|---|---|
| public Multibinder 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.
| |