Guice

Google 开源的依赖注入库,比 Spring 更小更快!

前提是你 提交于 2020-08-19 17:08:12
作者:GinoBeFunny https://zhuanlan.zhihu.com/p/24924391 Google开源的一个依赖注入类库 Guice ,相比于 Spring IoC 来说更小更快。 Elasticsearch大量使用了Guice,本文简单的介绍下Guice的基本概念和使用方式。 学习目标 概述:了解Guice是什么,有什么特点; 快速开始:通过实例了解Guice; 核心概念:了解Guice涉及的核心概念,如绑定(Binding)、范围(Scope)和注入(Injection); 最佳实践:官方推荐的最佳实践; Guice概述 Guice是Google开源的依赖注入类库,通过Guice减少了对工厂方法和new的使用,使得代码更易交付、测试和重用; Guice可以帮助我们更好地设计API,它是个轻量级非侵入式的类库; Guice对开发友好,当有异常发生时能提供更多有用的信息用于分析; 快速开始 假设一个在线预订Pizza的网站,其有一个计费服务接口: public interface BillingService { /** * 通过信用卡支付。无论支付成功与否都需要记录交易信息。 * * @return 交易回执。支付成功时返回成功信息,否则记录失败原因。 */ Receipt chargeOrder(PizzaOrder order, CreditCard

Google Guice can't find class

社会主义新天地 提交于 2020-06-18 11:25:17
问题 I was writing program using Google Guice, but I got exception! For testing I wrote a small program, but it didn't work too! There are all classes: Main.java: import com.google.inject.Guice; public class Main { public static void main(String[] args) { ITest test = Guice.createInjector(new TestModule()).getInstance(ITest.class); } } ITest.java: public interface ITest { } Test.java: public class Test implements ITest { } TestModule.java: import com.google.inject.AbstractModule; public class

Google Guice can't find class

眉间皱痕 提交于 2020-06-18 11:19:08
问题 I was writing program using Google Guice, but I got exception! For testing I wrote a small program, but it didn't work too! There are all classes: Main.java: import com.google.inject.Guice; public class Main { public static void main(String[] args) { ITest test = Guice.createInjector(new TestModule()).getInstance(ITest.class); } } ITest.java: public interface ITest { } Test.java: public class Test implements ITest { } TestModule.java: import com.google.inject.AbstractModule; public class

Google Guice can't find class

血红的双手。 提交于 2020-06-18 11:18:27
问题 I was writing program using Google Guice, but I got exception! For testing I wrote a small program, but it didn't work too! There are all classes: Main.java: import com.google.inject.Guice; public class Main { public static void main(String[] args) { ITest test = Guice.createInjector(new TestModule()).getInstance(ITest.class); } } ITest.java: public interface ITest { } Test.java: public class Test implements ITest { } TestModule.java: import com.google.inject.AbstractModule; public class

How to avoid Google guice injector getInstance() repetative calls due to null?

♀尐吖头ヾ 提交于 2020-06-01 05:14:29
问题 I have recently started using Google guice DI framework and am quite new to it. I am facing below problem - @inject returns null always and have to call injector to inject various references. This is my AbstractModule class public class AppModule extends AbstractModule { private static final Injector injector = Guice.createInjector(new AppModule()); private final Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions( new DropwizardMetricsOptions().setEnabled(true) )); private final

How to avoid Google guice injector getInstance() repetative calls due to null?

那年仲夏 提交于 2020-06-01 05:14:26
问题 I have recently started using Google guice DI framework and am quite new to it. I am facing below problem - @inject returns null always and have to call injector to inject various references. This is my AbstractModule class public class AppModule extends AbstractModule { private static final Injector injector = Guice.createInjector(new AppModule()); private final Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions( new DropwizardMetricsOptions().setEnabled(true) )); private final

Why is dagger considered better for AWS lambda implementation than Guice?

不想你离开。 提交于 2020-05-16 03:41:36
问题 I know that dagger creates injection at compile time by generating code and hence its performance is better than Guice, which do it at runtime. But specifically for case of lambda, I see it mentioned at multiple places that Dagger is preferred. Is it because of the cold start problem? Because of the cold start problem in lambda, lambda keeps doing bootstrapping multiple times whenever it receives a request after long time. So, with dagger, bootstrapping would be much faster as compared to

Hidden Features of Google Guice [closed]

…衆ロ難τιáo~ 提交于 2020-05-09 17:50:07
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Google Guice provides some great dependency injection features. I came across the @Nullable feature recently which allows you to mark

Hidden Features of Google Guice [closed]

邮差的信 提交于 2020-05-09 17:49:17
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Google Guice provides some great dependency injection features. I came across the @Nullable feature recently which allows you to mark

Java自动化测试框架-09

删除回忆录丶 提交于 2020-05-09 14:06:41
转自: https://www.cnblogs.com/du-hong/p/11818557.html 1.-依赖注入 TestNG支持两种不同类型的依赖项注入:本机(由TestNG本身执行)和外部(由诸如Guice的依赖项注入框架执行)。 1.1-本机依赖项注入 TestNG允许您在方法中声明其他参数。发生这种情况时,TestNG将自动用正确的值填充这些参数。依赖注入可以在以下地方使用: 任何@Before方法或@Test方法都可以声明ITestContext类型的参数。 任何@AfterMethod方法都可以声明ITestResult类型的参数,该参数将反映刚刚运行的测试方法的结果。 任何@Before和@After方法(@BeforeSuite和@AfterSuite除外)都可以声明XmlTest类型的参数,该参数包含当前的<test>标记。 任何@BeforeMethod(和@AfterMethod)都可以声明java.lang.reflect.Method类型的参数 。此参数将接收此@BeforeMethod完成之后(或在为@AfterMethod运行的方法之后)将调用的测试方法。 任何@BeforeMethod都可以声明Object []类型的参数。此参数将接收即将馈入即将到来的测试方法的参数列表,该参数列表可以由TestNG注入,例如java.lang.reflect