Skip to main content
edited tags
Link
Jeff Axelrod
  • 28.3k
  • 33
  • 152
  • 250
added a usage example
Source Link
Sean Carpenter
  • 7.7k
  • 3
  • 39
  • 39

I would like to be able to inject a generic implementation of a generic interface using Guice.

public interface Repository<T> { void save(T item); T get(int id); } public MyRepository<T> implements Repository<T> { @Override public void save(T item) { // do saving return item; } @Override public T get(int id) { // get item and return } } 

In C# using Castle.Windsor, I'd be able to do:

Component.For(typeof(Repository<>)).ImplementedBy(typeof(MyRepository<>)) 

but I don't think the equivalent exists in Guice. I know I can use TypeLiteral in Guice to register individual implementations, but is there any way to register them all at once like in Windsor?

Edit:

Here's an example of usage:

Injector injector = Guice.createInjector(new MyModule()); Repository<Class1> repo1 = injector.getInstance(new Key<Repository<Class1>>() {}); Repository<Class2> repo2 = injector.getInstance(new Key<Repository<Class2>>() {}); 

Although the more likely usage would be injection into another class:

public class ClassThatUsesRepository { private Repository<Class1> repository; @Inject public ClassThatUsesRepository(Repository<Class1> repository) { this.repository = repository; } } 

I would like to be able to inject a generic implementation of a generic interface using Guice.

public interface Repository<T> { void save(T item); T get(int id); } public MyRepository<T> implements Repository<T> { @Override public void save(T item) { // do saving return item; } @Override public T get(int id) { // get item and return } } 

In C# using Castle.Windsor, I'd be able to do:

Component.For(typeof(Repository<>)).ImplementedBy(typeof(MyRepository<>)) 

but I don't think the equivalent exists in Guice. I know I can use TypeLiteral in Guice to register individual implementations, but is there any way to register them all at once like in Windsor?

I would like to be able to inject a generic implementation of a generic interface using Guice.

public interface Repository<T> { void save(T item); T get(int id); } public MyRepository<T> implements Repository<T> { @Override public void save(T item) { // do saving return item; } @Override public T get(int id) { // get item and return } } 

In C# using Castle.Windsor, I'd be able to do:

Component.For(typeof(Repository<>)).ImplementedBy(typeof(MyRepository<>)) 

but I don't think the equivalent exists in Guice. I know I can use TypeLiteral in Guice to register individual implementations, but is there any way to register them all at once like in Windsor?

Edit:

Here's an example of usage:

Injector injector = Guice.createInjector(new MyModule()); Repository<Class1> repo1 = injector.getInstance(new Key<Repository<Class1>>() {}); Repository<Class2> repo2 = injector.getInstance(new Key<Repository<Class2>>() {}); 

Although the more likely usage would be injection into another class:

public class ClassThatUsesRepository { private Repository<Class1> repository; @Inject public ClassThatUsesRepository(Repository<Class1> repository) { this.repository = repository; } } 
Source Link
Sean Carpenter
  • 7.7k
  • 3
  • 39
  • 39

Inject Generic Implementation using Guice

I would like to be able to inject a generic implementation of a generic interface using Guice.

public interface Repository<T> { void save(T item); T get(int id); } public MyRepository<T> implements Repository<T> { @Override public void save(T item) { // do saving return item; } @Override public T get(int id) { // get item and return } } 

In C# using Castle.Windsor, I'd be able to do:

Component.For(typeof(Repository<>)).ImplementedBy(typeof(MyRepository<>)) 

but I don't think the equivalent exists in Guice. I know I can use TypeLiteral in Guice to register individual implementations, but is there any way to register them all at once like in Windsor?