【问题标题】:Named annotation failing in jar and source命名注释在 jar 和源中失败
【发布时间】:2014-07-07 20:35:09
【问题描述】:

openkad 项目有问题,但可能是 guice/环境问题:

要点是代码使用 Guice 来:

@Provides
@Singleton
@Named("openkad.rnd") 
Random provideRandom(@Named("openkad.seed") final long seed) {
        return seed == 0 ? new Random() : new Random(seed);
    }

但是@Singleton 和@Named("openkad.rnd") 都被编译器标记为不正确的位置。

显然是正确的,因为命名注释针对的是字段,而不是方法:

/**
 * Annotates named things.
 *
 * @author crazybob@google.com (Bob Lee)
 */
@Retention(RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@BindingAnnotation
public @interface Named {
  String value();
}

重新创建:

  1. 使用面向各种运行时的 JDK 1.6 或 1.8 在 eclipse 中创建 Java 项目
  2. 通过 pom 包含依赖项(guice 1 到 4 已尝试)
  3. 使用源代码或 openkad r60 jar 文件运行单元测试 (https://code.google.com/p/openkad/)

预期的输出是什么?你看到了什么?

所有 UT 都成功,但我看到的是这个:

com.google.inject.CreationException: Guice configuration errors:

1) Error at il.technion.ewolf.kbr.openkad.BootstrapNodesSaver.<init>(BootstrapNodesSaver.java:26):
 Binding to java.io.File annotated with @com.google.inject.name.Named(value=openkad.file.nodes) not found. No bindings to that type were found.

2) Error at il.technion.ewolf.kbr.openkad.IncomingContentHandler.<init>(IncomingContentHandler.java:43):
 Binding to java.util.concurrent.ExecutorService annotated with @com.google.inject.name.Named(value=openkad.executors.client) not found. No bindings to that type were found.

3) Error at il.technion.ewolf.kbr.openkad.KadNet.<init>(KadNet.java:75):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

4) Error at il.technion.ewolf.kbr.openkad.bucket.KadBuckets.<init>(KadBuckets.java:58):
 Binding to il.technion.ewolf.kbr.KeyFactory not found. No bindings to that type were found.

5) Error at il.technion.ewolf.kbr.openkad.handlers.ForwardHandler.<init>(ForwardHandler.java:96):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

6) Error at il.technion.ewolf.kbr.openkad.handlers.KademliaFindNodeHandler.<init>(KademliaFindNodeHandler.java:50):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

7) Error at il.technion.ewolf.kbr.openkad.handlers.PingHandler.<init>(PingHandler.java:38):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

8) Error at il.technion.ewolf.kbr.openkad.msg.ContentMessage.<init>(ContentMessage.java:26):
 Binding to long annotated with @com.google.inject.name.Named(value=openkad.rnd.id) not found. No bindings to that type were found.

9) Error at il.technion.ewolf.kbr.openkad.msg.ContentRequest.<init>(ContentRequest.java:27):
 Binding to long annotated with @com.google.inject.name.Named(value=openkad.rnd.id) not found. No bindings to that type were found.

10) Error at il.technion.ewolf.kbr.openkad.msg.FindNodeRequest.<init>(FindNodeRequest.java:25):
 Binding to long annotated with @com.google.inject.name.Named(value=openkad.rnd.id) not found. No bindings to that type were found.

11) Error at il.technion.ewolf.kbr.openkad.msg.ForwardRequest.<init>(ForwardRequest.java:30):
 Binding to long annotated with @com.google.inject.name.Named(value=openkad.rnd.id) not found. No bindings to that type were found.

12) Error at il.technion.ewolf.kbr.openkad.msg.PingRequest.<init>(PingRequest.java:22):
 Binding to long annotated with @com.google.inject.name.Named(value=openkad.rnd.id) not found. No bindings to that type were found.

13) Error at il.technion.ewolf.kbr.openkad.net.KadServer.<init>(KadServer.java:59):
 Binding to com.google.inject.Provider<java.net.DatagramSocket> annotated with @com.google.inject.name.Named(value=openkad.net.udp.sock) not found. No bindings to that type were found.

14) Error at il.technion.ewolf.kbr.openkad.net.KadServer.<init>(KadServer.java:59):
 Binding to java.util.concurrent.BlockingQueue<java.net.DatagramPacket> annotated with @com.google.inject.name.Named(value=openkad.net.buffer) not found. No bindings to that type were found.

15) Error at il.technion.ewolf.kbr.openkad.net.MessageDispatcher.<init>(MessageDispatcher.java:56):
 Binding to java.util.concurrent.BlockingQueue<il.technion.ewolf.kbr.openkad.net.MessageDispatcher<?>> annotated with @com.google.inject.name.Named(value=openkad.net.req_queue) not found. No bindings to that type were found.

16) Error at il.technion.ewolf.kbr.openkad.op.EagerColorFindValueOperation.<init>(EagerColorFindValueOperation.java:73):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

17) Error at il.technion.ewolf.kbr.openkad.op.JoinOperation.<init>(JoinOperation.java:48):
 Binding to il.technion.ewolf.kbr.Key annotated with @com.google.inject.name.Named(value=openkad.keys.zerokey) not found. No bindings to that type were found.

18) Error at il.technion.ewolf.kbr.openkad.op.KadFindNodeOperation.<init>(KadFindNodeOperation.java:49):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

19) Error at il.technion.ewolf.kbr.openkad.op.KadLocalCacheFindValueOperation.<init>(KadLocalCacheFindValueOperation.java:65):
 Binding to il.technion.ewolf.kbr.Node annotated with @com.google.inject.name.Named(value=openkad.local.node) not found. No bindings to that type were found.

19 error[s]
    at com.google.inject.BinderImpl.createInjector(BinderImpl.java:277)
    at com.google.inject.Guice.createInjector(Guice.java:79)
    at com.google.inject.Guice.createInjector(Guice.java:53)
    at com.google.inject.Guice.createInjector(Guice.java:43)
    at il.technion.ewolf.kbr.KeybasedRoutingTest.the2NodesShouldAbleToSendArbitrarySerializableMessages(KeybasedRoutingTest.java:314)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)

谢谢

【问题讨论】:

  • Guice 3 allows @Named on methods。尝试mvn dependency:tree -Dverbose 看看是否使用了冲突的 Guice 版本。
  • 就是这样,谢谢你让我看到那里!
  • 请记住将此问题标记为已解决。谢谢。

标签: java dependency-injection annotations guice


【解决方案1】:

Tavian Barnes 的建议奏效了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多