Service.java
import com.google.inject.Inject; import com.google.inject.name.Named; public class Service { @Inject //@Named("login timeout seconds") public int timeout; public void test(){ System.out.println("timeout is:" + timeout); }
Module.java
import com.google.inject.AbstractModule; import com.google.inject.name.Names; public class Module extends AbstractModule { public void configure() { bind(Integer.class).toInstance(10); //bind(Integer.class).annotatedWith(Names.named("login timeout seconds")).toInstance(10); bind(Service.class); } }
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()); Service service = injector.getInstance(Service.class); service.test(); } }