具有类型参数的Guice模块 [英] Guice module with type parameters

查看:87
本文介绍了具有类型参数的Guice模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间想知道是否可以编写一个guice模块
,它本身是用T类型参数化的,并且使用
的类型参数来指定绑定。



像这样(不工作)的例子:

  interface A< ; T> {} 
class AImpl< T>实现A< T> {}
接口B< T> {}
class BImpl< T>实现B< T> {}

class MyModule< T> (new TypeLiteral< AImpl< T>>扩展AbstractModule {
@Override
protected void configure(){
bind(new TypeLiteral< A< T> ;(){}); (new TypeLiteral< B< T>>(){})to(new TypeLiteral< BImpl< T>>(){});






$ b我尝试了不同的方法传递T到MyModule作为
Class / TypeLiteral的实例,但它们都没有工作。
帮助赞赏。



此致,ŁukaszOsipiuk

解决方案

使用 com.google.inject.util.Types 从头开始构建每个TypeLiteral。你可以这样做:

  class MyModule< T>扩展AbstractModule {
public MyModule(TypeLiteral< T> type){
_type = type;
}

@Override protected void configure(){
TypeLiteral< A< T>> a = newGenericType(A.class);
TypeLiteral< AImpl< T>> aimpl = newGenericType(AImpl.class);
绑定(a).to(aimpl);
TypeLiteral< B< T>> b = newGenericType(B.class);
TypeLiteral< BImpl< T>> bimpl = newGenericType(BImpl.class);
bind(b).to(bimpl);
}

@SuppressWarnings(unchecked)
private< V> TypeLiteral< V> newGenericType(Class<> base){
Type newType = Types.newParameterizedType(base,_type.getType());
return(TypeLiteral< V>)TypeLiteral.get(newType);
}

final private TypeLiteral< T> _类型;





$ b请注意私有方法newGenericType()将不对类型进行控制,这是你的职责,在 configure()中确保泛型类型可以用该方法正确构建。


I spent some time wondering if it is possible to write a guice module which itself is parametrized with type T and uses its type parameter to specify bindings.

Like in this (not working) example:

interface A<T> {} 
class AImpl<T> implements A<T>{} 
interface B<T> {} 
class BImpl<T> implements B<T> {} 

class MyModule<T> extends AbstractModule { 
    @Override 
    protected void configure() { 
        bind(new TypeLiteral<A<T>>(){}).to(new TypeLiteral<AImpl<T>>(){});
        bind(new TypeLiteral<B<T>>(){}).to(new TypeLiteral<BImpl<T>>(){}); 
    } 
} 

I tried different approaches passing trying to pass T to MyModule as instance of Class/TypeLiteral but none of them worked. Help appreciated.

Regards, Łukasz Osipiuk

解决方案

For that you will have to build each TypeLiteral from scratch, using com.google.inject.util.Types. You could do something like that:

class MyModule<T> extends AbstractModule {
    public MyModule(TypeLiteral<T> type) {
        _type = type;
    }

    @Override protected void configure() {
        TypeLiteral<A<T>> a = newGenericType(A.class);
        TypeLiteral<AImpl<T>> aimpl = newGenericType(AImpl.class);
        bind(a).to(aimpl);
        TypeLiteral<B<T>> b = newGenericType(B.class);
        TypeLiteral<BImpl<T>> bimpl = newGenericType(BImpl.class);
        bind(b).to(bimpl);
    }

    @SuppressWarnings("unchecked")
    private <V> TypeLiteral<V> newGenericType(Class<?> base) {
        Type newType = Types.newParameterizedType(base, _type.getType());
        return (TypeLiteral<V>) TypeLiteral.get(newType);
    }

    final private TypeLiteral<T> _type;
}

Please note that the private method newGenericType() will perform no control on types, it is your responsibility, in configure(), to make sure that generic types can be correctly built with that method.

这篇关于具有类型参数的Guice模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆