草庐IT

TypeLiteral

全部标签

java - 带反射的 TypeLiteral 注入(inject)

上下文:java使用guice(最新版本)大家好,是否可以通过这种方式向Guice注入(inject)一些TypeLiteral:publicMyClassgetMyClass(Injectorinjector,Classa,Classb){//howtoInjectMyClasswithtypea&b?//e.g:injector.getInstance(MyClass)}publicinterfaceMyClass{publicTdo(Ss);}publicclassClassOneimplementsMyClass{publicIntegerdo(Strings){//dosom

java - 绑定(bind)到 TypeLiteral 是 google guice 中的好习惯还是坏习惯

Googleguice使用newTypeLiteral>(){}克服我们不能使用C.class的事实.现在常见的有:bind(newTypeLiteral>(){}).to(MyCSubclassTypedToT.class);然而,想象一个不同的场景。我们有一个通用接口(interface),我们想要注入(inject)它,我们拥有的实现由一个通用类提供。Guice允许您这样做:bind(newTypeLiteral>(){}).to(newTypeLiteral>(){});另一种方法是像这样扩展MyGenericClass:MyTypedClassextendsMyGeneric

java - 带有类型参数的 Guice 模块

我花了一些时间想知道是否可以编写一个guice模块它本身用类型T参数化并使用它的类型参数指定绑定(bind)。就像这个(不工作的)例子:interfaceA{}classAImplimplementsA{}interfaceB{}classBImplimplementsB{}classMyModuleextendsAbstractModule{@Overrideprotectedvoidconfigure(){bind(newTypeLiteral>(){}).to(newTypeLiteral>(){});bind(newTypeLiteral>(){}).to(newTypeLit

java - 泛型 hell : Can I construct a TypeLiteral<Set<T>> using generics?

我能够让下面的通用方法工作的唯一方法是传递看似多余的TypeLiteral>范围。我相信应该可以在给定其他参数的情况下以编程方式构造此参数,但无法弄清楚如何。protectedKey>bindMultibinder(TypeLiteral>superClassSet,TypeLiteralsuperClass){finalKey>multibinderKey=Key.get(superClassSet,randomAnnotation);returnmultibinderKey;}客户端代码如下:bindMultibinder(newTypeLiteral>>(){},newTypeL

java - Guice:如何获得 TypeLiteral 包装的泛型的实例?

我有一个通用的数据库访问类,我正在使用TypeLiteral结构对其进行绑定(bind)。现在在测试中我想模拟那个类,因此我创建了一个Provider,它创建了一个模拟实例。在我的测试中,我想访问那个模拟来定义它的行为。现在的问题是,如何从注入(inject)器中检索对象?这是我的绑定(bind)定义:binder.bind(newTypeLiteral>(){}).GenericDbClassProvider.class);通常我会得到这样的实例:injector.getInstance(GenericDbClass.class);但是由于我没有将GenericDbClass的实现绑

java - 使用 Guice 注入(inject)通用实现

我希望能够使用Guice注入(inject)通用接口(interface)的通用实现。publicinterfaceRepository{voidsave(Titem);Tget(intid);}publicMyRepositoryimplementsRepository{@Overridepublicvoidsave(Titem){//dosavingreturnitem;}@OverridepublicTget(intid){//getitemandreturn}}在C#中使用CaSTLe.Windsor,我可以todo:Component.For(typeof(Repositor

java - 使用 Guice 注入(inject)通用实现

我希望能够使用Guice注入(inject)通用接口(interface)的通用实现。publicinterfaceRepository{voidsave(Titem);Tget(intid);}publicMyRepositoryimplementsRepository{@Overridepublicvoidsave(Titem){//dosavingreturnitem;}@OverridepublicTget(intid){//getitemandreturn}}在C#中使用CaSTLe.Windsor,我可以todo:Component.For(typeof(Repositor

java - Java中TypeLiteral的使用

请提供一些基本信息如何TypeLiteral在GoogleGuice或JavaEE中使用,如果使用简单的代码进行解释将非常有帮助,在此先感谢 最佳答案 目的TypeLiteral在Guice中,允许您将类和实例绑定(bind)到泛型类型(指定类型参数),避免因泛型未在Java中具体化这一事实而产生的问题,即删除隐藏了SomeInterface之间的差异这一事实。和SomeInterface在运行时。TypeLiteral通过创建通用类型的adhoc子类,允许通用参数的值在删除后继续存在。TypeLiteral的用法示例:bind(n