如何使用Guice进行以下工作?如何将特定参数绑定到自定义注释的实例?
// The Guice Module configuration
void configure() {
// The following won't compile because HelpTopicId is abstract.
// What do I do instead?
bind(new TypeLiteral<String>(){}).
annotatedWith(new HelpTopicId("A")).toInstance("1");
bind(new TypeLiteral<String>(){}).
annotatedWith(new HelpTopicId("B")).toInstance("2");
}
public @interface HelpTopicId {
public String helpTopicName();
}
public class Foo {
public Foo(@HelpTopicId("A") String helpTopicId) {
// I expect 1 and not 2 here because the actual parameter to @HelpTopicId is "A"
assertEquals(1, helpTopicId);
}
}
所有有效的点...仍然,'configure()'中的代码不会被编译。 – ripper234
我刚刚尝试过,'configure()'覆盖的方法正在编译。编译器给出的信息是什么? –
啊,对不起,我太快了,为了我自己的利益。我特别询问了自定义注释,而不是命名注释(我熟悉这些注释)。 – ripper234