Reputation: 230376
I'm trying to use Google Guice with the @Inject and @Singleton properties as follows:
I have:
configure()
method.The classes, constructor and interface are public, and still I'm getting the following error:
No implementation for IFoo was bound.
Upvotes: 3
Views: 8970
Reputation: 54615
You mean you get the error when doing this?
IFoo foo = injector.getInstance(IFoo.class);
Well then it is obvious. If the configure()
is empty how should guice know with what class to satisfy the dependency for IFoo
.
Just add this in the configure()
method and it should work. Now guice knows with what class to satisfy the dependency.
bind(IFoo.class).to(Foo.class);
If you don't want to configure this in the module you can annotate the interface. e.g.
@ImplementedBy(Foo.class)
public interface IFoo {
...
}
The @Singleton
annotations only tells guice to return the same instance for the class (the Singleton Pattern) everytime a request for the class is made via Injector.getInstance()
instead of creating a new instance everytime. But note that this is only a Singleton per Injector rather then per Classloader.
Upvotes: 6