• Guice 练习 instancebindings demo


    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);
        }
    View Code

    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);
        }
    }
    Module

    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();
        }
    }
    TestGuice
  • 相关阅读:
    Queries about less or equal elements CodeForces
    Session in BSU CodeForces
    基环树
    骑士 HYSBZ
    Valid BFS? CodeForces
    Trips CodeForces
    The writing on the wall 南京网络赛2018B题
    Building a Space Station
    Constructing Roads
    Networking
  • 原文地址:https://www.cnblogs.com/ms-grf/p/6703536.html
Copyright © 2020-2023  润新知