java - Guice injecting a generic list -
i inject list of various objects differentiate generic type.
i have mainview accepts list of contentpanels (a subtype of jpanel) want display.
so have
@inject mainview(list<contentpanel<?>> contentpanel){ ... }
the content panels differ in generic type, there 1 books, 1 movie etc.
i tried bind them
bind(new typeliteral<abstractcontentpanel<book>>(){}) .to(new typeliteral<bookcontentpanel<book>>(){}) .in(singleton.class);
and
bind(new typeliteral<abstractcontentpanel<movie>>(){}) .to(new typeliteral<bookcontentpanel<movie>>(){}) .in(singleton.class);
but how can make list of them , inject them mainview?
if want inject bindings list, cannot that. if want inject list, should bind directly, this:
bind(new typeliteral<list<string>>() {}) .toinstance(new arraylist<string>());
but in case list must known beforehand (or supplied via provider
).
if want access bindings through sequence, you'll have use multibinding extension. in case code this:
multibinder<contentpanel<?>> multibinder = multibinder.newsetbinder(binder(), new typeliteral<contentpanel<?>>() {}); multibinder.addbinding().to(yourcontentpanelimpl1.class); multibinder.addbinding().to(yourcontentpanelimpl2.class); // , on
and can inject set
:
@inject mainview(set<contentpanel<?>> contents) { ... }
Comments
Post a Comment