<dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.1.0</version> </dependency>
import com.google.inject.Binder; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; public class TestMoudle implements Module { @Override public void configure(Binder binder) { binder.bind(Add.class).to(SimpleAdd.class); } public static void main(String[] args) { Injector injector = Guice.createInjector(new TestMoudle()); Add instance = injector.getInstance(Add.class); instance.add(2,3); } }
public interface Add { void add(int a, int b); } public class SimpleAdd implements Add { @Override public void add(int a, int b) { System.out.println("a+b=" + (a + b)); } }