MultiBinder - injecting Collection<Provider<T>

272 views
Skip to first unread message

Suma Shivaprasad

unread,
Jun 11, 2015, 8:10:56 AM6/11/15
to google...@googlegroups.com
Trying to inject a collection of providers which implement an interface but consistently getting issues with injection as below

No implementation for java.util.Collection<com.google.inject.Provider<TypesChangeListener>> was bound.

[ERROR] at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
[ERROR] at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154)
[ERROR] at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)


Module
========
 Multibinder<TypesChangeListener> typesChangeListenerBinder = Multibinder.newSetBinder(binder(), TypesChangeListener.class);
typesChangeListenerBinder.addBinding().to(GraphBackedSearchIndexer.class);

// bind the MetadataService interface to an implementation
bind(MetadataService.class).to(DefaultMetadataService.class).asEagerSingleton();


Injected class
==============

@Inject
DefaultMetadataService(final B b, final C c, final Collection<Provider<TypesChangeListener>> typeChangeListeners)


GraphBackedSearchIndexer implements TypesChangeListener


I see that Multibinder<TypesChangeListener> typesChangeListenerBinder = Multibinder.newSetBinder(binder(), TypesChangeListener.class);

is actually resulting in Set<TypesChangeListener> rather than Set<Provider<TypesChangeListener>> which is what I need? 

The javadoc for MultiBinder - http://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/multibindings/Multibinder.html says "Collection<Provider<T>> could also be injected" but does not provide any examples.

Please let me know how this can be achieved.


Thanks
Suma


Stephan Classen

unread,
Jun 11, 2015, 8:14:19 AM6/11/15
to google...@googlegroups.com
I think you need to inject

Provider<Set<TypesChangeListener>> typeChangeListeners
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/d80493bd-c44d-450d-81a1-3b34229aab7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Broyer

unread,
Jun 11, 2015, 9:14:17 AM6/11/15
to google...@googlegroups.com
Which version are you using? This will only work starting with 4.0-beta5: https://github.com/google/guice/commit/4faa20e3081448792933834aedfe972add806292

Suma Shivaprasad

unread,
Jun 11, 2015, 11:07:05 AM6/11/15
to google...@googlegroups.com
Am using guice 3.0 . Is there any other way of injecting a set of interface implementations as provider apart from upgrading since the one having it is still in beta ?

Stephan Classen

unread,
Jun 11, 2015, 11:09:28 AM6/11/15
to google...@googlegroups.com
4.0 has been released about a month ago
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.

Suma Shivaprasad

unread,
Jun 11, 2015, 12:16:48 PM6/11/15
to google...@googlegroups.com
Thanks :). Works with upgrade to 4.0 !


On Thursday, June 11, 2015 at 5:40:56 PM UTC+5:30, Suma Shivaprasad wrote:
Reply all
Reply to author
Forward
0 new messages