I am wondering what the difference is between using the @provides method and using bind() in my modules.
I usually override AbstractModule.configure () and bind all my implementations to my interfaces as follows:
public class MyModule extends AbstractModule { @Override protected void configure() { this.bind(myIface.class).to(myIfaceImpl.class); this.bind(myOtherIface.class).to(myOtherIfaceImpl.class).asEagerSingleton(); } ... }
However, I noticed a template in the code base I'm working with now, where implementations are not explicitly linked, they are returned from such providers:
public class MyModule extends AbstractModule { @Provides @Singleton myIface iFaceProvider() { return new myIfaceImpl(); } ... }
Is there any reason to prefer each other? Are there any cases that invoke a particular method?
java dependency-injection guice
Ben glasser
source share