Tag: guice

Guice – 如何通过多个注入器/模块共享同一个Singleton实例

在guice中,@ Singleton范围不涉及Singleton模式。 根据“Dhanji”的“dependency injection”一书: 非常简单,单例的上下文就是注入器本身。 单身的寿命与注射器的寿命有关(如图5.8所示)。 因此,每个注射器只创建一个单例实例。 重要的是要强调最后一点,因为多个喷射器可能存在于同一应用中。 在这种情况下,每个注入器将保存单例范围对象的不同实例。 是否可以通过多个模块和多个喷射器共享同一个Singleton实例?

如何在gradle依赖项的依赖项中指定分类器?

假设我想在项目中添加guice-assistedinject作为依赖项。 它将guice工件指定为依赖项本身。 如何告诉它使用no_aop版本的guice? 我知道我可以做到以下几点,但是如果不排除guice模块,我可以一步完成吗? dependencies { compile (group: ‘com.google.inject.extensions’, name: ‘guice-assistedinject’, version: ‘3.0’) { exclude module: ‘guice’ } compile group: ‘com.google.inject’, name: ‘guice’, version: ‘3.0’, classifier: ‘no_aop’ }

Generics Hell:我可以使用generics构建TypeLiteral <Set >吗?

我能够使下面的generics方法工作的唯一方法是传递看似冗余的TypeLiteral<Set>参数。 我认为应该可以在给定其他参数的情况下以编程方式构造此参数,但无法弄清楚如何。 protected Key<Set> bindMultibinder( TypeLiteral<Set> superClassSet, TypeLiteral superClass) { final Key<Set> multibinderKey = Key.get(superClassSet, randomAnnotation); return multibinderKey; } 客户端代码如下: bindMultibinder(new TypeLiteral<Set<A>>(){}, new TypeLiteral<A>(){}); 其中A和B是接口。 如果我尝试以下(删除TypeLiteral<Set> superClassSet参数),我得到一个java.util.Set cannot be used as a key; It is not fully specified. java.util.Set cannot be used as a key; It is not fully specified. 运行时错误。 protected Key<Set> bindMultibinder(TypeLiteral superClass) […]

什么是CDI的实例或Guices Provider的Spring等价物

在CDI中,您可以使用以下命令定义一个对象,该对象将为您提供特定类型的项目: @注入 Instance myObjectInstance; // … MyObject myObjectInstance.get(); 同样在Guice中你可以这样做: @注入 Provider myObjectInstance; // … MyObject myObjectInstance.get(); 我想知道Spring中是否有类似的构造,或者您必须使用ApplicationContext才能获得引用?

用Guice注入generics

我正在尝试迁移一个小项目,用Guice替换一些工厂(这是我的第一个Guice试验)。 但是,在尝试注射仿制药时,我陷入困境。 我设法提取了一个带有两个类和一个模块的小玩具示例: import com.google.inject.Inject; public class Console { private final StringOutput out; @Inject public Console(StringOutput out) { this.out = out; } public void print(T t) { System.out.println(out.converter(t)); } } public class StringOutput { public String converter(T t) { return t.toString(); } } import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.TypeLiteral; public class MyModule extends […]

具有多个实现的Guice和接口

如果我有接口Validator和这个接口的多个实现。 如何使用Guice注入任何多个实现? 现在我知道我可以使用以下代码注入一个,但它只允许一个实现: public class MyModule extends AbstractModule { @Override protected void configure() { bind(Validator.class).to(OneOfMyValidators.class); } } 我想做的是: Validator v1 = injector.getInstance(Validator1.class); Validator v2 = injector.getInstance(Validator2.class); 有可能吗?

dependency injection:按区域划分范围(Guice,Spring,Whatever)

这是我需求的简化版本。 我有一个程序,其中每个B对象都有自己的C和D对象,通过Guice注入。 此外,A对象被注入每个C和D对象。 我想要的是 :对于每个B对象,它的C和D对象将被注入相同的A对象。 [编辑-开始] (1)Guice支持“单例”和“原型”模式。 但是,我需要的是介于两者之间:我需要A对给定的B对象进行单例WRT(这样注入B对象的C和D将共享一个A对象)。 对于另一个B对象,我想要另一个A.所以它是一个单例,但对于程序的有限范围(实际上,数据结构的范围有限)。 (2)我不介意使用方法(setter)或现场注入的解决方案。 (3)我曾多次尝试实现这一点,并且总觉得我只需要实现DI容器的一些自定义东西来完成这项工作 – 但它从未奏效。 因此,我正在寻找一个详细的解决方案(而不仅仅是“挥手”) [编辑-完] 具体来说,我希望程序的输出(如下)为: Created C0 with [A0] Created D0 with [A0] Created B0 with [C0, D0] Created C1 with [A1] Created D1 with [A1] Created B1 with [C1, D1] 它当前产生以下输出的位置: Created C0 with [A0] Created D0 with [A1] <– Should be A0 […]

哪个Java Web Framework最适合Google Guice?

我打算开始一个新项目,并且正在研究当前最先进的Java Web框架。 我决定围绕Guice构建我的应用程序,并且可能使用非常轻量级的ORM,如Squill / JEQUEL / JaQu或类似的,但我无法决定Web框架。 哪一种最适合这种轻量级环境? 哪一个最好与Guice整合?

如何使用Google Guice创建需要参数的对象?

也许我只是盲目,但我不知道如何使用Guice(刚刚开始)来替换此方法中的new调用: public boolean myMethod(String anInputValue) { Processor proc = new ProcessorImpl(anInputValue); return proc.isEnabled(); } 为了测试,处理器可能有不同的实现,所以我想避免new调用,并在此过程中摆脱对实现的依赖。 如果我的类只记得处理器的一个实例,我可以通过构造函数注入它,但由于处理器被设计为不可变的,我每次都需要一个新的。 我将如何使用Guice(2.0)实现这一目标?

Java Guice – 如何将整数值绑定到我的类/对象?

我不明白如何在绑定类时简单地绑定任何值。 我总是得到这个错误: No implementation for test.Triangle annotated with @com.google.inject.name.Named(value=triangle) was bound. 我试过这个: 三角类 @Inject public void setLength(@Named(“triangle”) int length) { this.length = length; } 配置类 bind(Triangle.class).annotatedWith(Names.named(“triangle”)).toInstance(1); //this one just gives error that I can’t do that. 如何在其中输入值,因此它将使用setLength方法和我选择的值?..我读了Guice文档,但没有找到它。 在Spring框架中,像这样的东西似乎更容易做和理解(也许它有更好的文档,至少对我来说)。 此外,如果我错过了文档,它显示了这种事情,你也可以链接它。