Service.java
public interface Service { public void test();}
AService.java
public class AService implements Service { public void test(){ System.out.println("A tested!"); }}
OtherServiceA.java
public class OtherServiceA { public Service service; public OtherServiceA(Service service) { this.service = service; } public void test(){ service.test(); }}
Module.java
public class Module extends AbstractModule { public void configure() { bind(Service.class).to(AService.class); try { bind(OtherServiceA.class).toConstructor(OtherServiceA.class.getConstructor(Service.class)) ; } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }}
TestGuice.java
import com.google.inject.Guice;import com.google.inject.Injector;import org.junit.Test;public class TestGuice { @Test public void test() { Injector injector = Guice.createInjector(new Module()); OtherServiceA otherServiceA = injector.getInstance(OtherServiceA.class); otherServiceA.test(); }}