java - 使用 HK2 在泛型类中注入(inject)类型参数

标签 java jersey hk2

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

代码如下:

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();

关于java - 使用 HK2 在泛型类中注入(inject)类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40672630/

相关文章:

java - 使用 enctype ="multipart/form-data"时请求 getParameter 始终为 null

java - Java EE 项目中的 Log4j2 配置文件

java - Jersey HK2 依赖注入(inject)在更新到 v2.27 后不起作用

java - 如何在 jersey 2.15 中启用 HK2 TopicDistributionService?

java - 在某些类中关闭其他库的 logback 日志记录

java - 在 Scala 中创建一个 Json 数组

java - 如何防止 Eclipse 在将表达式分配给局部变量时生成类型注释

java - 调试 Jersey 框架

tomcat - JAX-RS/Jersey + Tomcat ServletContext 为空

java - org.glassfish.jersey.process.internal.RequestScoped 有多个 Activity 上下文