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.
解决方法:
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();
标签:jersey,hk2,java 来源: https://codeday.me/bug/20191112/2024200.html