java di/ioc

public class Test {

    @org.junit.Test
    public void test1() {
        HK2 hk2 = HK2.get();
        Services services = hk2.create(null, new Module[0]);
        DynamicBinderFactory dynamicBinderFactory = services.bindDynamically();

        Binder binder = dynamicBinderFactory.bind(ServiceImpl.class, ServiceImpl.class, Service.class);
        ResolvedBinder resolvedBinder = binder.to(ServiceImpl.class);
        resolvedBinder.in(Singleton.class);

        binder = dynamicBinderFactory.bind(ServiceImpl1.class);
        resolvedBinder = binder.to(ServiceImpl1.class);
        resolvedBinder.in(Singleton.class);

        binder = dynamicBinderFactory.bind(ServiceImpl2.class);
        resolvedBinder = binder.to(ServiceImpl2.class);
        resolvedBinder.in(Singleton.class);

        dynamicBinderFactory.commit();

        ServiceLocator serviceLocator = services.byType(ServiceImpl.class);
        ServiceImpl serviceImpl = (ServiceImpl) serviceLocator.get();
        serviceImpl.name();

        serviceLocator = services.byType(ServiceImpl1.class);
        ServiceImpl1 serviceImpl1 = (ServiceImpl1) serviceLocator.get();
        serviceImpl1.name();

        serviceLocator = services.byType(ServiceImpl2.class);
        ServiceImpl2 serviceImpl2 = (ServiceImpl2) serviceLocator.get();
        serviceImpl2.name();

        System.out.println();
    }

    @org.junit.Test
    public void test2() {
        HK2 hk2 = HK2.get();
        Services services = hk2.create(null, new Module[0]);
        DynamicBinderFactory dynamicBinderFactory = services.bindDynamically();

        Binder binder = dynamicBinderFactory.bind(Service.class);
        ResolvedBinder resolvedBinder = binder.to(ServiceImpl.class);
        resolvedBinder.in(Singleton.class);

        binder = dynamicBinderFactory.bind(ServiceImpl.class);
        resolvedBinder = binder.to(ServiceImpl.class);
        resolvedBinder.in(Singleton.class);

        binder = dynamicBinderFactory.bind(ServiceImpl1.class);
        resolvedBinder = binder.to(ServiceImpl1.class);
        resolvedBinder.in(Singleton.class);

        binder = dynamicBinderFactory.bind(ServiceImpl2.class);
        resolvedBinder = binder.to(ServiceImpl2.class);
        resolvedBinder.in(Singleton.class);

        dynamicBinderFactory.commit();

        ServiceLocator serviceLocator = services.byType(ServiceImpl.class);
        ServiceImpl serviceImpl = (ServiceImpl) serviceLocator.get();
        serviceImpl.name();

        serviceLocator = services.byType(ServiceImpl1.class);
        ServiceImpl1 serviceImpl1 = (ServiceImpl1) serviceLocator.get();
        serviceImpl1.name();

        serviceLocator = services.byType(ServiceImpl2.class);
        ServiceImpl2 serviceImpl2 = (ServiceImpl2) serviceLocator.get();
        serviceImpl2.name();

        System.out.println();
    }
}


public class Test {

    public static void main(String[] args) {
        ServiceLocator locator = ServiceLocatorFactory.getInstance().create("default");
        DynamicConfigurationService dynamicConfigurationService = locator.getService(DynamicConfigurationService.class);
        DynamicConfiguration config = dynamicConfigurationService.createDynamicConfiguration();

        config.bind(BuilderHelper.link(ServiceImpl.class).to(ServiceImpl.class).to(Service.class).in(Singleton.class).build());
        config.bind(BuilderHelper.link(ServiceImpl1.class).to(ServiceImpl1.class).in(Singleton.class).build());
        config.bind(BuilderHelper.link(ServiceImpl2.class).to(ServiceImpl2.class).in(Singleton.class).build());

        config.commit();

        ServiceLocator newLocator = ServiceLocatorFactory.getInstance().find("default");

        ServiceImpl service = locator.getService(ServiceImpl.class);
        System.out.println(service);
        service.name();

        ServiceImpl1 service1 = locator.getService(ServiceImpl1.class);
        System.out.println(service1);
        service1.name();

        ServiceImpl2 service2 = locator.getService(ServiceImpl2.class);
        System.out.println(service2);
        service2.name();
    }
}

猜你喜欢

转载自lobin.iteye.com/blog/2431622
今日推荐