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 {private final T t;@Injectpublic 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() {@Overrideprotected void configure() {bind(B.class).to(B.class);bindAsContract(new TypeLiteral>() {}); // <--- ???}});serviceLocator.getService(new TypeLiteral>() {}.getType());}}
此代码导致以下异常:
Exception in thread "main" MultiException stack 1 of 3
java.lang.IllegalArgumentException: Invalid injectee with required type of T passed to getInjecteeDescriptorat 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 foundat 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$Aat 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>() {}.getType();ActiveDescriptor> ad = BuilderHelper.activeLink(A.class).to(type).asType(type).build();
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
