Reputation: 1945
I have a provider:
@SuppressWarnings({ "rawtypes", "unchecked" })
@Provides
@Singleton
@OutboundBroker
public EventBroker outboundBrokerProvider()
At runtime, I want to get this one.
EventBroker outbound=injector.getInstance(Key.get(EventBroker.class, Names.named("OutboundBroker")));
However, this code doesn't work -- the provider isn't named, but I can't figure out how to retrieve it using the annotation @OutboundBroker
Upvotes: 2
Views: 321
Reputation: 24286
It's extremely rare to need to get more than one object from the Injector
. If you use pure dependency-injection, your main()
method (or your container) creates the injector and gets one object, and one or more methods on that object are called. That one object would be injected with what it needs.
So you can do this:
class EventService {
private final EventBroker outboundEventBroker;
@Inject
private EventService(
@OutboundBroker EventBroker outboundBroker) {
this.outboundEventBroker = outboundBroker;
}
...
}
As Jeff Bowman stated, this assumes that OutboundBroker
is itself annotated with @BindingAnnotation
.
Upvotes: 0