使用 HK2 在泛型 class 中注入类型参数

Injection of type parameter in a generic class with HK2

我目前正在玩 HK2 2.5.0-b05(这是 Jersey 2.24 使用的版本),我无法执行特定类型的注入。我能够概括我的问题,并提出了一个简单的小型测试用例。

代码如下:

package com.github.fabriziocucci.test;

import javax.inject.Inject;

import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.glassfish.hk2.utilities.binding.AbstractBinder;

public class Test {

    private static class A<T> {

        private final T t;

        @Inject
        public A(T t) {
            this.t = t;
            System.out.println(t);
        }

    }

    private static class B {

    }

    public static void main(String[] args) {
        ServiceLocator serviceLocator = ServiceLocatorFactory.getInstance().create(null);
        ServiceLocatorUtilities.bind(serviceLocator, new AbstractBinder() {
            @Override
            protected void configure() {
                bind(B.class).to(B.class);
                bindAsContract(new TypeLiteral<A<B>>() {}); // <--- ???
            }
        });

        serviceLocator.getService(new TypeLiteral<A<B>>() {}.getType());
    }

}

此代码导致以下异常:

Exception in thread "main" MultiException stack 1 of 3
java.lang.IllegalArgumentException: Invalid injectee with required type of T passed to getInjecteeDescriptor
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetInjecteeDescriptor(ServiceLocatorImpl.java:546)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getInjecteeDescriptor(ServiceLocatorImpl.java:585)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:70)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:228)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:713)
    at com.github.fabriziocucci.test.Test.main(Test.java:39)
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.github.fabriziocucci.test.Test$A errors were found
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:246)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:713)
    at com.github.fabriziocucci.test.Test.main(Test.java:39)
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on com.github.fabriziocucci.test.Test$A
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:386)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2020)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:766)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:713)
    at com.github.fabriziocucci.test.Test.main(Test.java:39)

我确定我的代码有问题,但我不知道是什么。

更新 1

我刚刚用 hk2-2.5.0-b28 进行了相同的测试,除非我的代码包含一些细微的错误,否则结果是一样的。

可能会有更多更新 here

hk2 的这项新功能现已在版本 hk2-2.5.0-b29 中可用。

您可以在您的示例中使用 bindAsContract,但您也可以直接在 ActiveDescriptor 上设置类型或在 EDSL 中使用新的 "asType",例如:

Type type = new TypeLiteral<A<B>>() {}.getType();

ActiveDescriptor<?> ad = BuilderHelper.activeLink(A.class).
            to(type).
            asType(type).build();