com.google.inject.Guice.createInjector()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(189)

本文整理了Java中com.google.inject.Guice.createInjector()方法的一些代码示例,展示了Guice.createInjector()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Guice.createInjector()方法的具体详情如下:
包路径:com.google.inject.Guice
类名称:Guice
方法名:createInjector

Guice.createInjector介绍

[英]Creates an injector for the given set of modules, in a given development stage.
[中]在给定的开发阶段,为给定的模块集创建喷油器。

代码示例

代码示例来源:origin: apache/incubator-gobblin

protected Optional<JobHistoryStore> createJobHistoryStore(Properties jobProps) {
 boolean jobHistoryStoreEnabled = Boolean
   .valueOf(jobProps.getProperty(ConfigurationKeys.JOB_HISTORY_STORE_ENABLED_KEY, Boolean.FALSE.toString()));
 if (jobHistoryStoreEnabled) {
  Injector injector = Guice.createInjector(new MetaStoreModule(jobProps));
  return Optional.of(injector.getInstance(JobHistoryStore.class));
 } else {
  return Optional.absent();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testMultiConditionalBind_cat()
{
 props.setProperty("animal.type", "cat");
 Injector injector = Guice.createInjector(new Module()
 {
  @Override
  public void configure(Binder binder)
  {
   ConditionalMultibind.create(props, binder, Animal.class)
             .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("cat"), Cat.class)
             .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("dog"), Dog.class);
  }
 });
 Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>()
 {
 }));
 Assert.assertEquals(1, animalSet.size());
 Assert.assertEquals(animalSet, ImmutableSet.<Animal>of(new Cat()));
}

代码示例来源:origin: killbill/killbill

@Test(groups = "fast")
public void testAOPForClass() throws Exception {
  // Make sure it works as expected without any AOP magic
  final IAopTester simpleTester = new AopTester();
  try {
    simpleTester.createRefund();
  } catch (Exception e) {
    Assert.fail(e.getLocalizedMessage());
  }
  // Now, verify the interception works
  configureShiro();
  final Injector injector = Guice.createInjector(Stage.PRODUCTION,
                          new ShiroModuleNoDB(configSource),
                          new KillBillShiroAopModule(),
                          new TestSecurityModuleNoDB(configSource),
                          new CacheModule(configSource),
                          new AbstractModule() {
                            @Override
                            protected void configure() {
                              bind(IDBI.class).toInstance(Mockito.mock(IDBI.class));
                              bind(IDBI.class).annotatedWith(Names.named(MAIN_RO_DATA_SOURCE_ID)).toInstance(Mockito.mock(IDBI.class));
                              bind(TenantInternalApi.class).toInstance(Mockito.mock(TenantInternalApi.class));
                              bind(NonEntityDao.class).toInstance(Mockito.mock(NonEntityDao.class));
                            }
                          });
  final AopTester aopedTester = injector.getInstance(AopTester.class);
  verifyAopedTester(aopedTester);
}

代码示例来源:origin: jclouds/legacy-jclouds

@Test
public void testWithoutConfigAllIdsMatch() {
 Set<String> regionIds = ImmutableSet.of("us-east-1", "eu-west-1");
  RegionIdFilter filter = Guice.createInjector(new AbstractModule(){
   @Override
   protected void configure() {
    bindConstant().annotatedWith(Provider.class).to("aws-ec2");
   }
    }).getInstance(AnyOrConfiguredRegionId.class);
 assertEquals(Sets.filter(regionIds, filter), ImmutableSet.of("us-east-1", "eu-west-1"));
}

代码示例来源:origin: jclouds/legacy-jclouds

public void test() {
   ParseObjectFromHeadersAndHttpContent parser = Guice.createInjector().getInstance(
      ParseObjectFromHeadersAndHttpContent.class);
   AtmosObject data = parser.apply(RESPONSE);

   assertEquals(data, EXPECTED);
  }
}

代码示例来源:origin: com.google.inject.extensions/guice-servlet

public void testScopeExceptions() throws Exception {
 Injector injector =
   Guice.createInjector(
     new AbstractModule() {
      @Override
  injector.getInstance(String.class);
  fail();
 } catch (ProvisionException oose) {
  assertContains(oose.getMessage(), "Cannot access scoped [java.lang.String].");
  injector.getInstance(Integer.class);
  fail();
 } catch (ProvisionException oose) {
  assertContains(oose.getMessage(), "Cannot access scoped [java.lang.Integer].");
 Key<?> key = Key.get(String.class, Names.named("foo"));
 try {
  injector.getInstance(key);
  fail();
 } catch (ProvisionException oose) {
  assertContains(oose.getMessage(), "Cannot access scoped [" + Errors.convert(key) + "]");

代码示例来源:origin: com.google.inject.extensions/guice-servlet

public void testIsRequestScopedPositive() {
 final Key<String> a = Key.get(String.class, named("A"));
 final Key<String> b = Key.get(String.class, named("B"));
 final Key<String> c = Key.get(String.class, named("C"));
 final Key<String> d = Key.get(String.class, named("D"));
 final Key<Object> e = Key.get(Object.class, named("E"));
 assertTrue(ServletScopes.isRequestScoped(map.get(g)));
 Injector injector = Guice.createInjector(requestScopedBindings, new ServletModule());
 assertTrue(ServletScopes.isRequestScoped(injector.getBinding(a)));
 assertTrue(ServletScopes.isRequestScoped(injector.getBinding(b)));

代码示例来源:origin: jclouds/legacy-jclouds

@Test
public void testThere() {
 assertEquals(Guice.createInjector(new AbstractModule() {
   @Override
   protected void configure() {
    bindConstant().annotatedWith(Names.named("foo")).to("bar");
   }
 }).getInstance(ValueOfConfigurationKeyOrNull.class).apply("foo"), "bar");
}

代码示例来源:origin: Netflix/Priam

@BeforeClass
public static void setup() throws InterruptedException, IOException {
  new MockNodeProbe();
  injector = Guice.createInjector(new BRTestModule());
  filesystem =
      (FakeBackupFileSystem)
          injector.getInstance(
              Key.get(IBackupFileSystem.class, Names.named("backup")));
}

代码示例来源:origin: prestodb/presto

@BeforeMethod
public void startUp()
{
  Injector injector = Guice.createInjector(new JsonModule(), new HandleJsonModule());
  objectMapper = injector.getInstance(ObjectMapper.class);
}

代码示例来源:origin: killbill/killbill

@Test(groups = "fast")
public void testAOPForInterface() throws Exception {
  // Make sure it works as expected without any AOP magic
  final IAopTester simpleTester = new AopTesterImpl();
  try {
    simpleTester.createRefund();
  } catch (Exception e) {
    Assert.fail(e.getLocalizedMessage());
  }
  // Now, verify the interception works
  configureShiro();
  final Injector injector = Guice.createInjector(Stage.PRODUCTION,
                          new ShiroModuleNoDB(configSource),
                          new KillBillShiroAopModule(),
                          new TestSecurityModuleNoDB(configSource),
                          new CacheModule(configSource),
                          new AbstractModule() {
                            @Override
                            public void configure() {
                              bind(IDBI.class).toInstance(Mockito.mock(IDBI.class));
                              bind(IDBI.class).annotatedWith(Names.named(MAIN_RO_DATA_SOURCE_ID)).toInstance(Mockito.mock(IDBI.class));
                              bind(IAopTester.class).to(AopTesterImpl.class).asEagerSingleton();
                              bind(TenantInternalApi.class).toInstance(Mockito.mock(TenantInternalApi.class));
                              bind(NonEntityDao.class).toInstance(Mockito.mock(NonEntityDao.class));
                            }
                          });
  final IAopTester aopedTester = injector.getInstance(IAopTester.class);
  verifyAopedTester(aopedTester);
}

代码示例来源:origin: jclouds/legacy-jclouds

public void testExcluder() {
 Json excluder = Guice.createInjector(new GsonModule(), new AbstractModule() {
   protected void configure() {
    bind(DefaultExclusionStrategy.class).to(ExcludeStringValue.class);
   }
 }).getInstance(Json.class);
 ObjectNoDefaultConstructor obj = new ObjectNoDefaultConstructor("foo", 1);
 assertEquals(excluder.toJson(obj), "{\"intValue\":1}");
}

代码示例来源:origin: com.google.inject.extensions/guice-servlet

public void testIsRequestScopedNegative() {
 final Key<String> a = Key.get(String.class, named("A"));
 final Key<String> b = Key.get(String.class, named("B"));
 final Key<String> c = Key.get(String.class, named("C"));
 final Key<String> d = Key.get(String.class, named("D"));
 final Key<String> e = Key.get(String.class, named("E"));
 assertFalse(ServletScopes.isRequestScoped(map.get(j)));
 Injector injector = Guice.createInjector(requestScopedBindings);
 assertFalse(ServletScopes.isRequestScoped(injector.getBinding(a)));
 assertFalse(ServletScopes.isRequestScoped(injector.getBinding(b)));

代码示例来源:origin: jclouds/legacy-jclouds

@Test
  public void testEmptyIsThere() {
   assertEquals(Guice.createInjector(new AbstractModule() {

     @Override
     protected void configure() {
      bindConstant().annotatedWith(Names.named("foo")).to("");
     }

   }).getInstance(ValueOfConfigurationKeyOrNull.class).apply("foo"), "");
  }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testMultiConditionalBind_cat_dog()
{
 props.setProperty("animal.type", "pets");
 Injector injector = Guice.createInjector(new Module()
 {
  @Override
  public void configure(Binder binder)
  {
   ConditionalMultibind.create(props, binder, Animal.class)
             .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class)
             .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
  }
 });
 Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>()
 {
 }));
 Assert.assertEquals(2, animalSet.size());
 Assert.assertEquals(animalSet, ImmutableSet.of(new Cat(), new Dog()));
}

代码示例来源:origin: prestodb/presto

@BeforeMethod
public void startUp()
{
  Injector injector = Guice.createInjector(new JsonModule(), new HandleJsonModule());
  objectMapper = injector.getInstance(ObjectMapper.class);
}

代码示例来源:origin: jclouds/legacy-jclouds

@Test
  public void testExpand() {
   Properties input = new Properties();
   input.setProperty("id", "1234");
   input.setProperty("path", "path:${id}");
   Properties output = Guice.createInjector(new BindPropertiesToExpandedValues(input)).getInstance(Properties.class);

   Properties expected = new Properties();
   expected.setProperty("id", "1234");
   expected.setProperty("path", "path:1234");
   assertEquals(output, expected);
  }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testMultiConditionalBind_cat_dog_non_continuous_syntax()
{
 props.setProperty("animal.type", "pets");
 Injector injector = Guice.createInjector(new Module()
 {
  @Override
  public void configure(Binder binder)
  {
   ConditionalMultibind.create(props, binder, Animal.class)
             .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Cat.class);
   ConditionalMultibind.create(props, binder, Animal.class)
             .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("pets"), Dog.class);
  }
 });
 Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>()
 {
 }));
 Assert.assertEquals(2, animalSet.size());
 Assert.assertEquals(animalSet, ImmutableSet.of(new Cat(), new Dog()));
}

代码示例来源:origin: apache/incubator-druid

@Test
 public void testPropertiesWithRestrictedConfigs()
 {
  Injector injector = Guice.createInjector(Collections.singletonList(new PropertiesModule(Collections.singletonList(
    "status.resource.test.runtime.properties"))));
  Map<String, String> returnedProperties = injector.getInstance(StatusResource.class).getProperties();
  Set<String> hiddenProperties = new HashSet<>();
  Splitter.on(",").split(returnedProperties.get("druid.server.hiddenProperties")).forEach(hiddenProperties::add);
  hiddenProperties.forEach((property) -> Assert.assertNull(returnedProperties.get(property)));
 }
}

代码示例来源:origin: jclouds/legacy-jclouds

@Test
public void testWithoutConfigAllIdsMatch() {
 Set<String> zoneIds = ImmutableSet.of("us-east-1a", "us-east-1b");
  ZoneIdFilter filter = Guice.createInjector(new AbstractModule(){
   @Override
   protected void configure() {
    bindConstant().annotatedWith(Provider.class).to("aws-ec2");
   }
    }).getInstance(AnyOrConfiguredZoneId.class);
 assertEquals(Sets.filter(zoneIds, filter), ImmutableSet.of("us-east-1a", "us-east-1b"));
}

相关文章