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

我目前正在使用HK2 2.5.0-b05(泽西岛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.

Inm小程序商店 | Vultr中文网

最佳答案

hk2的此新功能现已在hk2-2.5.0-b29版本中可用.

 

您可以像在示例中一样使用bindAsContract,但也可以直接在ActiveDescriptor上设置Type或在EDSL中使用新的“ asType”,例如:

 

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

ActiveDescriptor<?> ad = BuilderHelper.activeLink(A.class).
            to(type).
            asType(type).build();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值