本文整理汇总了Java中com.google.inject.Injector类的典型用法代码示例。如果您正苦于以下问题:Java Injector类的具体用法?Java Injector怎么用?Java Injector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Injector类属于com.google.inject包,在下文中一共展示了Injector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.google.inject.Injector; //导入依赖的package包/类
/**
* Start the micro service.
*/
public static void main(final String[] args) {
try {
final Injector injector =
Guice.createInjector(new ElasticSearchModule());
//start http server in daemon mode
injector
.getInstance(HttpServer.class)
.start();
//start grpc server
injector
.getInstance(GrpcServer.class)
.start()
// blocks until VM shutdown begins
.awaitTermination();
} catch (Exception ex) {
log.error("failed to start ip-elasticsearch service ", ex);
throw new IllegalStateException("failed to start elasticsearch service ", ex);
}
}
示例2: setUp
import com.google.inject.Injector; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
String ip = esContainer.getContainerIpAddress();
Integer transportPort = esContainer.getMappedPort(9300);
MapConfiguration memoryParams = new MapConfiguration(new HashMap<>());
memoryParams.setProperty(CONFIG_ES_CLUSTER_HOST, ip);
memoryParams.setProperty(CONFIG_ES_CLUSTER_PORT, transportPort);
memoryParams.setProperty(CONFIG_ES_CLUSTER_NAME, "elasticsearch");
Injector injector = Guice.createInjector(
Modules.override(new ElasticSearchModule()).with(
binder -> {
binder.bind(Configuration.class).toInstance(memoryParams);
}
)
);
transportClientProvider = injector.getInstance(TransportClientProvider.class);
}
示例3: testLazy
import com.google.inject.Injector; //导入依赖的package包/类
@Test
public void testLazy() {
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
this.bind(Thing.class).to(ThingA.class);
this.bindLazy(Thing.class).annotatedWith(Names.named("b")).to(ThingB.class);
this.bindLazy(Thing.class).annotatedWith(Names.named("c")).to(ThingC.class);
}
});
final Things things = injector.getInstance(Things.class);
assertNotSame(things.ap.get(), things.ap.get());
assertSame(things.al.get(), things.al.get());
assertSame(things.bl.get(), things.bl.get());
assertSame(things.cl.get(), things.cl.get());
assertSame(things.dl.get(), things.dl.get());
}
示例4: create
import com.google.inject.Injector; //导入依赖的package包/类
@Override
public Connector create(String connectorId, Map<String, String> config, ConnectorContext context)
{
requireNonNull(config, "config is null");
try {
Bootstrap app = new Bootstrap(
new JsonModule(),
new HDFSModule(connectorId, context.getTypeManager())
);
Injector injector = app
.strictConfig()
.doNotInitializeLogging()
.setRequiredConfigurationProperties(config)
.initialize();
return injector.getInstance(HDFSConnector.class);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例5: load
import com.google.inject.Injector; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public Resource load(final ResourceSet resourceSet, final URI uri, final InputStream input) throws IOException {
final Injector injector = IXtInjectorProvider.INSTANCE.getInjector(context.get(XpectJavaModel.class), uri);
final Resource resource = injector.getInstance(IResourceFactory.class).createResource(uri);
final Resource existingResousce = from(resourceSet.getResources())
.firstMatch(r -> r.getURI().equals(resource.getURI())).orNull();
if (null != existingResousce) {
// remove the existing one
resourceSet.getResources().remove(existingResousce);
}
resourceSet.getResources().add(resource);
try {
resource.load(input, null);
} finally {
if (input != null)
input.close();
}
return resource;
}
示例6: createInjectorAndDoEMFRegistration
import com.google.inject.Injector; //导入依赖的package包/类
@Override
public Injector createInjectorAndDoEMFRegistration() {
// register default ePackages
if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("ecore"))
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"ecore", new EcoreResourceFactoryImpl());
if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xmi"))
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"xmi", new XMIResourceFactoryImpl());
if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xtextbin"))
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"xtextbin", new BinaryGrammarResourceFactoryImpl());
if (!EPackage.Registry.INSTANCE.containsKey(XtextPackage.eNS_URI))
EPackage.Registry.INSTANCE.put(XtextPackage.eNS_URI, XtextPackage.eINSTANCE);
Injector injector = createInjector();
register(injector);
return injector;
}
示例7: testInstances
import com.google.inject.Injector; //导入依赖的package包/类
@Test public void testInstances() throws Exception {
Injector injector = WebAppTests.createMockInjector(this);
HttpServletRequest req = injector.getInstance(HttpServletRequest.class);
HttpServletResponse res = injector.getInstance(HttpServletResponse.class);
String val = req.getParameter("foo");
PrintWriter out = res.getWriter();
out.println("Hello world!");
logInstances(req, res, out);
assertSame(req, injector.getInstance(HttpServletRequest.class));
assertSame(res, injector.getInstance(HttpServletResponse.class));
assertSame(this, injector.getInstance(TestWebAppTests.class));
verify(req).getParameter("foo");
verify(res).getWriter();
verify(out).println("Hello world!");
}
示例8: canCreateRecordBrowserProvider
import com.google.inject.Injector; //导入依赖的package包/类
@Test
public void canCreateRecordBrowserProvider() throws IOException, URISyntaxException {
String command = RECORD;
Map<String, Object> opts = new HashMap<>();
opts.put(DRIVER, chromedriverFile.getAbsolutePath());
opts.put(APPLICATION, configurationFile.getAbsolutePath());
opts.put(URL, localhostUrl);
opts.put(OUTPUT, "output.json");
opts.put(BROWSER, CHROME);
opts.put(TIMEOUT, timeoutString);
opts.put(SCREEN, screenString);
Module module = new DefaultModule(command, opts);
Injector injector = Guice.createInjector(module);
IOURIProvider<RecordBrowser> instance = injector.getInstance(new Key<IOURIProvider<RecordBrowser>>() {});
RecordBrowser recordBrowser = instance.get();
// cleanup
recordBrowser.cleanUp();
}
示例9: lifeCycleStarted
import com.google.inject.Injector; //导入依赖的package包/类
@Override
public void lifeCycleStarted(final LifeCycle event) {
LOGGER.info("Starting End Point Health server");
final Injector injector = (Injector) servletContext.getAttribute(Injector.class.getName());
endPointCheckSchedulerService = Optional.ofNullable(injector.getInstance(EndPointCheckSchedulerService.class));
if (endPointCheckSchedulerService.isPresent()) {
endPointCheckSchedulerService.get().start();
} else {
throw new EndPointHealthException("Unable to get EndPointCheckService instance");
}
LOGGER.info("End Point Health server started");
}
示例10: testLogsView2
import com.google.inject.Injector; //导入依赖的package包/类
@Test
public void testLogsView2() throws IOException {
LOG.info("HsLogsPage with data");
MockAppContext ctx = new MockAppContext(0, 1, 1, 1);
Map<String, String> params = new HashMap<String, String>();
params.put(CONTAINER_ID, MRApp.newContainerId(1, 1, 333, 1)
.toString());
params.put(NM_NODENAME,
NodeId.newInstance(MockJobs.NM_HOST, MockJobs.NM_PORT).toString());
params.put(ENTITY_STRING, "container_10_0001_01_000001");
params.put(APP_OWNER, "owner");
Injector injector =
WebAppTests.testPage(AggregatedLogsPage.class, AppContext.class, ctx,
params);
PrintWriter spyPw = WebAppTests.getPrintWriter(injector);
verify(spyPw).write(
"Aggregation is not enabled. Try the nodemanager at "
+ MockJobs.NM_HOST + ":" + MockJobs.NM_PORT);
}
示例11: getByIdInputTest
import com.google.inject.Injector; //导入依赖的package包/类
/**
* Creates a token for accessing the REST API via username/password
* <p>
* The token returned is formatted as a JSON Web Token (JWT). The token is base64 encoded and comprised of three parts. The header, the body, and the signature. The expiration of the token is a contained within the body. The token can be used in the Authorization header in the format 'Authorization: Bearer <token>'.
*
* @throws ApiException if the Api call fails
*/
@Test
public void getByIdInputTest() throws ApiException, IOException, URISyntaxException {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(InputPortsApi.class).toInstance(inputPortsApiMock);
bind(OutputPortsApi.class).toInstance(outputPortsApiMock);
bind(Integer.class).annotatedWith(Names.named("timeout")).toInstance(1);
bind(Integer.class).annotatedWith(Names.named("interval")).toInstance(1);
bind(Boolean.class).annotatedWith(Names.named("forceMode")).toInstance(false);
}
});
PortService portService = injector.getInstance(PortService.class);
PortEntity port = new PortEntity();
port.setComponent(new PortDTO());
port.getComponent().setId("id");
when(inputPortsApiMock.getInputPort("id")).thenReturn(port);
PortEntity portResult = portService.getById("id", PortDTO.TypeEnum.INPUT_PORT);
assertEquals("id", portResult.getComponent().getId());
}
示例12: getInjector
import com.google.inject.Injector; //导入依赖的package包/类
private Injector getInjector() {
Injector injector = null;
try {
injector = CollectorInjector.createInjector(new ConfigurationModule(configFile),
new BufferModule(),
new InputsModule(),
new FileModule(),
new OutputsModule(),
new ServicesModule(),
new MetricsModule(),
new MemoryReporterModule(),
new ServerApiModule(),
new HeartbeatModule(),
new CollectorIdModule(),
new CollectorHostNameModule());
} catch (Exception e) {
LOG.error("ERROR: {}", e.getMessage());
LOG.debug("Detailed injection creation error", e);
doExit();
}
return injector;
}
示例13: GuiceGroupByFactory
import com.google.inject.Injector; //导入依赖的package包/类
@Inject
@SuppressWarnings("unchecked")
public GuiceGroupByFactory(Injector injector)
{
this.injector = injector;
Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();
for (Key<?> key : bindings.keySet())
{
Class<?> bindingClass = key.getTypeLiteral().getRawType();
if (GroupBy.class.isAssignableFrom(bindingClass))
{
GroupByName name = (GroupByName)bindingClass.getAnnotation(GroupByName.class);
if (name == null)
throw new IllegalStateException("Aggregator class "+bindingClass.getName()+
" does not have required annotation "+GroupByName.class.getName());
groupBys.put(name.name(), (Class<GroupBy>)bindingClass);
}
}
}
示例14: testFactory
import com.google.inject.Injector; //导入依赖的package包/类
@Test
public void testFactory() {
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
this.installFactory(FooThingFactory.class, builder -> builder.implement(FooThing.class, FooThingImpl.class));
}
});
final FooThings things = injector.getInstance(FooThings.class);
assertEquals(100, things.factory.create(100).value());
}
示例15: getInjector
import com.google.inject.Injector; //导入依赖的package包/类
public Injector getInjector(String language) {
synchronized (injectors) {
Injector injector = injectors.get(language);
if (injector == null) {
injectors.put(language, injector = createInjector(language));
}
return injector;
}
}