We saved binding collections for this chapter, because this is something, which requires use of Providers, we will introduce MultiBinder for binding Guice managed objects.
Let's start with a simple example, where in we need to inject objects that are not managed by Guice. Let's try to inject a Set<String>
messages in FlightEngine. Define an attribute, and annotate the setter with injector.
@Inject public void setMessages(Set<String> messages) { this.messages = messages; }
Next, we need to write a provider for the same:
public class MessageProvider implements Provider<Set<String>> { @Override public Set<String> get() { Set<String> messageSet = new HashSet<String>(); messageSet.add("Hi Client"); messageSet.add("Bye Client"); return messageSet; } }
The provider should be bound using a TypeLiteral:
bind(new TypeLiteral<Set<String>>(){}).toProvider(MessageProvider.class).in(Singleton...