Guice generics - how can I make it less ugly?

I have an interface Producer and a concrete FooProducer that implements Producer. Binding this in guice looks ugly as sin:

bind(new TypeLiteral<Producer<Foo>>() {}).to(FooProducer.class);

I have lots of these such bindings. I have tried the following:

static <T> TypeLiteral<Producer<T>> producer() {
    return new TypeLiteral<Producer<T>>(){};
}

With calls made in this way:

bind(ContainingClass.<Foo>producer()).to(FooProducer.class);

But it gives an error along the lines of Producer is not specific enough....

Am I going about this in the wrong way?

8
задан skaffman 18 May 2011 в 18:10
поделиться