Multibindings extension

66 views
Skip to first unread message

limpb...@gmail.com

unread,
May 1, 2008, 6:05:39 AM5/1/08
to google-guice
As previously alluded on my blog, I've started work on the
Multibindings API. I've checked in the initial implementation, and I'm
pretty excited about it. You can grab it from Subversion, in the
extensions/multibindings directory.

This feature is ideal for lightweight plugin-type architectures, where
you've got multiple modules each contributing Servlets, Actions,
Filters, Components or even just names.

Here's the class Javadoc:


An API to bind multiple values separately, only to later inject them
as a complete collection. Multibinder is intended for use in your
application's module:

public class SnacksModule extends AbstractModule {
protected void configure() {
Multibinder<Snack> multibinder
= Multibinder.newSetBinder(binder(), Snack.class);
multibinder.addBinding().toInstance(new Twix());
multibinder.addBinding().toProvider(SnickersProvider.class);
multibinder.addBinding().to(Skittles.class);
}
}
With this binding, a Set<Snack> can now be injected:

class SnackMachine {
@Inject
public SnackMachine(Set<Snack> snacks) { ... }
}
Create multibindings from different modules is supported. For example,
it is okay to have both CandyModule and ChipsModule to both create
their own Multibinder<Snack>, and to each contribute bindings to the
set of snacks. When that set is injected, it will contain elements
from both modules.

Elements are resolved at set injection time. If an element is bound to
a provider, that provider's get method will be called each time the
set is injected (unless the binding is also scoped).

Annotations are be used to create different sets of the same element
type. Each distinct annotation gets its own independent collection of
elements.

Elements must be distinct. If multiple bound elements have the same
value, set injection will fail.

Elements must be non-null. If any set element is null, set injection
will fail.

--
Jesse / http://publicobject.com/

maaxiim

unread,
May 2, 2008, 7:22:10 AM5/2/08
to google-guice
On May 1, 6:05 am, "je...@swank.ca" <limpbiz...@gmail.com> wrote:
> As previously alluded on my blog, I've started work on the
> Multibindings API. I've checked in the initial implementation, and I'm
> pretty excited about it. You can grab it from Subversion, in the
> extensions/multibindings directory.
>
<snip>
> --
> Jesse /http://publicobject.com/

It works perfectly!
I took the latest rev from svn and have replaced my kludge for
building a list of requirements processors with the much cleaner
multibinder syntax.
Fantastic extension and sincere thanks to you.


-- maaxiim
"A hospital bed is a parked taxi with the meter running." -- Groucho
Marx
Reply all
Reply to author
Forward
0 new messages