Using MultiBinder programmaticlly.

103 views
Skip to first unread message

Jochen Wiedmann

unread,
Sep 25, 2013, 7:24:35 AM9/25/13
to google...@googlegroups.com

Hi,

I have read about the MultiBinder. As such, my application contains code like this:

        Multibinder<LifecycleAware> mb = Multibinder.newSetBinder(binder, LifecycleAware.class);
        mb.addBinding().to(DbInitializer.class);

However, now I'd like to have omthing like this:

        public List<LifecycleAware> getLifecycleAwares(Injector pInjector) {
        }

In other words, I'd like to query the Injector programmatically for implementations of LifecycleAware.

How would I do this?

Thanks,

Jochen



Johannes Wachter

unread,
Sep 25, 2013, 12:48:23 PM9/25/13
to google...@googlegroups.com
Hi Jochen,

to query for the correct result you can just use a Key based on the correct TypeLiteral for the multi bound instances.

In your example this would be:

Set<LifecycleAware> lifecycleAwares = pInjector.getInstance(Key.get(new TypeLiteral<Set<LifecycleAware>>(){}));

If your getLifecycleAwares() method really needs to be a List instead of a Set, you need to wrap/copy the retrieved Set, e.g. through Guavas ImmutableList.copyOf(lifecycleAwares) or something similar.

Regards,
Johannes


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Jochen Wiedmann

unread,
Sep 28, 2013, 2:54:27 PM9/28/13
to google...@googlegroups.com
Thanks a lot, Johannes,

that did the trick nicely. Personally, I'd like Guice to make this kind of stuff less tricky, but it works, and that's the key.

Jochen

Tim Boudreau

unread,
Sep 30, 2013, 2:35:49 AM9/30/13
to google...@googlegroups.com
You can certainly do what's recommended here, but I find it's usually more readable and flexible to write some sort of "registry" of things, and invoke that - certainly if there's any chance you'll need to consume the collection in more than one place, or perform the same operation on all elements of it.

If you do that, you can then either keep using multi-binding for registration, or you can have a register() method on your "registry", and bind the things that must register themselves as eager singletons and have them register themselves in their constructor.

If my guess about what "LifecycleAware" could be is right, this may be similar to what you are writing:
and it has the additional advantage that you can call it programmatically from a test harness to do cleanup on test completion rather than VM shutdown.

-Tim

Jochen Wiedmann

unread,
Oct 7, 2013, 3:33:50 AM10/7/13
to google...@googlegroups.com
Thanks for the additional hint. Your guess is close, except that I am trying to handle the startup phase, not the shutdown phase. So, I'm still in search for something a little bit different.

Jochen



--
You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/EUnNStmrhOk/unsubscribe.
To unsubscribe from this group and all its topics, 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.
For more options, visit https://groups.google.com/groups/opt_out.



--
"That's what prayers are ... it's frightened people trying to make friends with the bully!"

Terry Pratchett. The Last Hero

Reply all
Reply to author
Forward
0 new messages