I’m trying to use Google Guice with the @Inject and @Singleton properties as follows:
I have:
- A Module with an empty
configure()method. - An interface IFoo
- A class Foo (implementing IFoo), annotated with @Singleton, with a parameter-less constructor annotated with @Inject. This is the single annotated constructor.
The classes, constructor and interface are public, and still I’m getting the following error:
No implementation for IFoo was bound.
You mean you get the error when doing this?
Well then it is obvious. If the
configure()is empty how should guice know with what class to satisfy the dependency forIFoo.Just add this in the
configure()method and it should work. Now guice knows with what class to satisfy the dependency.If you don’t want to configure this in the module you can annotate the interface. e.g.
The
@Singletonannotations only tells guice to return the same instance for the class (the Singleton Pattern) everytime a request for the class is made viaInjector.getInstance()instead of creating a new instance everytime. But note that this is only a Singleton per Injector rather then per Classloader.