use of com.google.inject.Binder in project druid by druid-io.
the class CacheMonitorTest method testOptionalInject.
@Test
public void testOptionalInject() throws Exception {
Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {
@Override
public void configure(Binder binder) {
JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
}
}));
CacheMonitor monitor = injector.getInstance(CacheMonitor.class);
Assert.assertNull(monitor.cache);
}
use of com.google.inject.Binder in project druid by druid-io.
the class MockMemcachedClient method testSimpleInjection.
@Test
public void testSimpleInjection() {
final String uuid = UUID.randomUUID().toString();
System.setProperty(uuid + ".type", "memcached");
System.setProperty(uuid + ".hosts", "localhost");
final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(new Module() {
@Override
public void configure(Binder binder) {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/memcached");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bind(Cache.class).toProvider(CacheProvider.class);
JsonConfigProvider.bind(binder, uuid, CacheProvider.class);
}
}));
final CacheProvider memcachedCacheProvider = injector.getInstance(CacheProvider.class);
Assert.assertNotNull(memcachedCacheProvider);
Assert.assertEquals(MemcachedCacheProvider.class, memcachedCacheProvider.getClass());
}
use of com.google.inject.Binder in project druid by druid-io.
the class MockMemcachedClient method testBasicInjection.
@Test
public void testBasicInjection() throws Exception {
final MemcachedCacheConfig config = new MemcachedCacheConfig() {
@Override
public String getHosts() {
return "127.0.0.1:22";
}
};
Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {
@Override
public void configure(Binder binder) {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/memcached");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bind(MemcachedCacheConfig.class).toInstance(config);
binder.bind(Cache.class).toProvider(MemcachedProviderWithConfig.class).in(ManageLifecycle.class);
}
}));
Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
lifecycle.start();
try {
Cache cache = injector.getInstance(Cache.class);
Assert.assertEquals(MemcachedCache.class, cache.getClass());
} finally {
lifecycle.stop();
}
}
use of com.google.inject.Binder in project druid by druid-io.
the class ComposingEmitterModuleTest method testGetEmitterViaRealGuice.
@Test
public void testGetEmitterViaRealGuice() {
Injector injector = Guice.createInjector(new DruidGuiceExtensions(), new LifecycleModule(), new Module() {
@Override
public void configure(Binder binder) {
Properties props = new Properties();
props.put("druid.emitter.composing.emitters", "[\"" + testEmitterType + "\"]");
binder.bind(Properties.class).toInstance(props);
binder.bind(Validator.class).toInstance(Validation.buildDefaultValidatorFactory().getValidator());
binder.bind(Emitter.class).annotatedWith(Names.named(testEmitterType)).toInstance(emitter);
}
}, new ComposingEmitterModule());
injector.getInstance(Key.get(Emitter.class, Names.named("composing"))).start();
EasyMock.verify(emitter);
}
use of com.google.inject.Binder in project JessMA by ldcsaa.
the class MyGuiceInjectFilter method configModules.
@Override
protected Collection<Module> configModules() {
Set<Module> modules = new HashSet<Module>();
// JDBC DAO Binding Module
modules.add(new Module() {
@Override
public void configure(Binder binder) {
// 配置绑定规则
binder.bind(dao.jdbc.UserDao.class).toInstance(FacadeProxy.create(dao.jdbc.UserDao.class));
}
});
// MyBatis DAO Binding Module
modules.add(new Module() {
@Override
public void configure(Binder binder) {
// 配置绑定规则
binder.bind(dao.mybatis.UserDao.class).toInstance(FacadeProxy.create(dao.mybatis.UserDao.class));
}
});
// Hibernate DAO Binding Module
modules.add(new Module() {
@Override
public void configure(Binder binder) {
// 配置绑定规则
binder.bind(dao.hbn.UserDao.class).toInstance(FacadeProxy.create(dao.hbn.UserDao.class));
}
});
return modules;
}
Aggregations