guice 辅助注入 + 多重绑定 + 泛型

And*_*rew 6 java generics guice multibinding assisted-inject

我正在尝试结合 Guice 的这 3 个特性:注入、多重绑定、泛型。我创建了一个生产项目的原型,所以这里是:

首先,这是泛型的一个小层次结构(在生产情况下有 N 个实体的层次结构):

    public interface Type {
    }
    public class Type1 implements Type{
    }
    public class Type2 implements Type {
    }
Run Code Online (Sandbox Code Playgroud)

接下来,我想通过Factory创建类ToCreate1ToCreate2

基类:

    public abstract class AbstractToCreate<T extends Type> {
        public T type;
        public Integer param;

        public AbstractToCreate(T type, Integer param){
            this.type = type;
            this.param = param;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是继承人:

    public class ToCreate1 extends AbstractToCreate<Type1>{
        @Inject
        public ToCreate1(Type1 type, @Assisted Integer param) {
            super(type, param);
        }  
    }

   public class ToCreate2 extends AbstractToCreate<Type2> {
        @Inject
        public ToCreate2(Type2 type, @Assisted Integer param) {
            super(type, param);
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后,工厂本身:

    public interface Factory<T extends Type> {
        AbstractToCreate<T> create(Integer param);
    }
Run Code Online (Sandbox Code Playgroud)

所以,现在我想注入一个包含Factory<Type1>Factory<Type2> 的地图来分别创建ToInject1ToInject2

所以,我用 configure 方法创建了 Guice 的AbstractModule

    protected void configure() {
            install(new FactoryModuleBuilder()
                    .implement(new TypeLiteral<AbstractToCreate<Type1>>(){}, ToCreate1.class)
                    .build(new TypeLiteral<Factory<Type1>>(){}));                     
            install(new FactoryModuleBuilder()
                    .implement(new TypeLiteral<AbstractToCreate<Type2>>(){}, ToCreate2.class)
                    .build(new TypeLiteral<Factory<Type2>>(){}));

            MapBinder<String, Factory> mapBinder = MapBinder.newMapBinder(binder(), String.class, Factory.class);
            mapBinder.addBinding("type1").to(new TypeLiteral<Factory<Type1>>(){});
            mapBinder.addBinding("type2").to(new TypeLiteral<Factory<Type2>>(){});
        }
Run Code Online (Sandbox Code Playgroud)

所以,我注入它@Inject public Map<String, Factory> map;,一切正常:

    Factory<Type1> factory1 = main.map.get("type1");
    Factory<Type2> factory2 = main.map.get("type2");

    AbstractToCreate<Type1> create1 = factory1.create(1);//create1 is ToCreate1 instance
    AbstractToCreate<Type2> create2 = factory2.create(2);//create2 is ToCreate2 instance
Run Code Online (Sandbox Code Playgroud)

正如我之前提到的,我的生产系统中有更多的类型,因此AbstractModule变得过于繁琐。我试图避免重复代码并修改配置方法:

    @Override
    protected void configure() {
        this.<Type1>inst(ToCreate1.class);
        this.<Type2>inst(ToCreate2.class);
    }

    private <V extends Type> void inst(Class<? extends AbstractToCreate<V>> clazz) {
        install(new FactoryModuleBuilder()
                .implement(new TypeLiteral<AbstractToCreate<V>>(){}, clazz)
                .build(new TypeLiteral<Factory<V>>(){}));
    }
Run Code Online (Sandbox Code Playgroud)

它不起作用!Guice 说:

1) ru.test.genericassistedinject.AbstractToCreate<V> cannot be used as a key; It is not fully specified. 
Run Code Online (Sandbox Code Playgroud)

怎么了?

jac*_*obm 5

这里的问题是类型擦除。特别是,这段代码:

private <V extends Type> void inst(Class<? extends AbstractToCreate<V>> clazz) {
    install(new FactoryModuleBuilder()
            .implement(new TypeLiteral<AbstractToCreate<V>>(){}, clazz)
            .build(new TypeLiteral<Factory<V>>(){}));
}
Run Code Online (Sandbox Code Playgroud)

无法工作,因为它依赖类型参数V来帮助做出运行时决策(使用什么绑定),但类型参数V没有运行时表示,因此它的值永远不会直接影响运行时。另一种思考方式是:Java 无法“读取”泛型中类型参数的值;无论调用者中实例化new TypeLiteral<Factory<V>>(){}什么,它始终是相同的值。V

当您遇到与擦除相关的问题时,通常会遇到这种情况,技巧是添加一个表示您想要的类型的运行时值。在这种情况下,这尤其棘手,因为您想要做的是将类型参数的值表示为更大的类型。

有几种方法可以获取表示静态类型的运行时值。TypeToken是一个,Class也是另一个,但是它们都不允许您用参数表示类型,然后以编程方式填充该值。幸运的是,Google Guava 包含另一个com.google.common.reflect.TypeToken对我们有用的表示形式 。TypeTokens 可以表示带有变量的类型,并支持以编程方式用具体表示形式“填充”该变量,例如:

new TypeToken<List<V>>() {}.where(new TypeParameter<V>() {}, Integer.class)
Run Code Online (Sandbox Code Playgroud)

代表运行时的类型List<Integer>

使用TypeToken我们可以构建我们的类型,如下所示:

 private <V extends Type> void inst(Class<? extends AbstractToCreate<V>> clazz, Class<V> binding) {
    TypeToken<AbstractToCreate<V>> implementationType = new TypeToken<AbstractToCreate<V>>() {}
        .where(new TypeParameter<V>() {}, binding);
    TypeToken<Factory<V>> factoryType = new TypeToken<Factory<V>>() {}
        .where(new TypeParameter<V>() {}, binding);

    @SuppressWarnings("unchecked")  // The type returned by TypeToken::getType is always the type it represents
    Key<AbstractToCreate<V>> key = (Key<AbstractToCreate<V>>) Key.get(implementationType.getType());
    @SuppressWarnings("unchecked")  // as above
    Key<Factory<V>> factoryKey = (Key<Factory<V>>) Key.get(factoryType.getType());

    install(
        new FactoryModuleBuilder()
            .implement(key, clazz)
            .build(factoryKey));
  }
Run Code Online (Sandbox Code Playgroud)

现在我们可以调用inst

inst(ToCreate1.class, Type1.class);
inst(ToCreate2.class, Type2.class);
Run Code Online (Sandbox Code Playgroud)

一切都会按预期进行。

不过,这是非常奇特的东西,理解它取决于对类型的编译时和运行时表示之间的差异有很好的理解。如果是我,如果你只希望使用一两次,我就不会这样做,因为混淆负担相当高;只有当这是图书馆或其他东西的一部分并且您可以为每个调用者节省一些工作时,我才会这样做。

  • 对此答案+1,但除非您已经依赖 Guava,否则您可能可以使用 [`Types.newParameterizedType`](https://google.github.io/guice/api-docs/最新/javadoc/com/google/inject/util/Types.html#newParameterizedType-java.lang.reflect.Type-java.lang.reflect.Type...-) 代替。也查看其余类型;它是 TypeToken API 的微型补充。 (2认同)