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.