Couple of comments.
1. Resolving generic type dependencies can be done without named
annotations. The trick is to use a Guice TypeLiteral. See:
http://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/TypeLiteral.html
Your bind becomes something more like:
bind(new TypeLiteral<GenericInterface<String>>() {}).toInstance(new
GenericClass<String>());
2. You probably want to use the multibinder extension to inject all
implementations into a single location. See:
https://github.com/google/guice/wiki/Multibindings
Your binding becomes more like:
Multibinder<GenericInterface> mBinder = Multibinder.newSetBinder(binder(),
GenericInterface.class);
mBinder.addBinding().toInstance(new GenericClass<String>());
3. Instead of using multibinder you might enjoy something of an
auto-registration approach.
public abstract class BaseGenericInterface<T> extends GenericInterface<T> {
@Inject
public void registerThis(GenericInterfaceRegistry registry) {
registry.registerThis(this);
}
}
I like this approach the most because in Scala I can actually put the
method (and implementation) in the interface and it just automagically
happens. (When I do this, I also almost always pass a scala
manifest/typetag to the registry too.)
Happy guicing!
Nate
On Tue Feb 10 2015 at 2:47:11 PM Karsten Schulz <
[email protected]> wrote:
> Hello guys,
>
> i have a handling question about guice. i like really dependency injection
> and use this in the most of private projects. But currently i have some
> problems with generics and multiple binding for one interface. For the
> first problem i have another solution. i don't know if thats works, but i
> will test it in the future.
>
> So the second problem is for me currently a little bit difficult to
> understand.
>
> The starting point is:
>
> I have an Interface thats supports Generic, for Example:
> public interface GenericInterface<T> {}
>
> So i have also an implemented class, for Example:
> public class GenericClass<T> implements GenericInterface<T> {}
>
> So this interface and class have some generic methods.
>
> In my Modul i define some bindings:
>
> public class ExampleModule extends AbstractModule {
>
> public void configure() {
> bind(GenericInterface.class).annotatedWith(Names.named("Example"
> )).toInstance(new GenericClass<String>());
> bind(GenericInterface.class).annotatedWith(Names.named("Second")).
> toInstance(new GenericClass<Integer>());
> }
> }
>
> Now my Question. In my opinion, i have to different instances from
> GenericInterface. So i can inject this with the annotation @Named
>
> But in another case, i have a class, that will add all implementations of
> GenericInterface without specify the name.
>
> public class ExmapleClass {
>
> @Inject
> public <T> void addGeneric(GenericInterface<T> genericInterface) {}
> }
>
> Ok first, i find out, that generic methods doesn't work, but it's also not
> possible to add automaticly all GenericInterfaces to this method. So i
> searching for an solutions, that has the feature like the ServiceTracker
> functionality from OSGi.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/293f68ba-b645-493d-9dcb-1f6165564e2f%40googlegroups.com
> <https://groups.google.com/d/msgid/google-guice/293f68ba-b645-493d-9dcb-1f6165564e2f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
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/CAHNex9_-BVXZ%2BkcWtV1HboVD28qXQhWd4B5iymPeeFLo9rLo-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.