如何将特定参数绑定到自定义注释的实例?

发布于 2024-11-18 12:29:17 字数 669 浏览 2 评论 0原文

如何使用 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);
  }
}

How do I make the following work using 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);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2024-11-25 12:29:17

也许最简单的方法是使用 @Provides 方法:

@Provides @HelpTopicId("A")
protected String provideA() {
  return "1";
}

或者,您可以创建 HelpTopicId 注释/接口的可实例化实现,类似于 的实现>Names.named(请参阅NamedImpl)。请注意,对于如何为注释实现 hashCode() 等内容有一些特殊规则...NamedImpl 遵循这些规则。

另外,使用 new TypeLiteral(){} 是浪费...可以使用 String.class 来代替它。此外,对于 Stringint 等,您通常应该使用 bindConstant() 而不是 bind(String.class).它更简单,要求您提供绑定注释,并且仅限于基元、StringClass 文字和enum

Probably the simplest way to do this would be to use @Provides methods:

@Provides @HelpTopicId("A")
protected String provideA() {
  return "1";
}

Alternatively, you could create an instantiable implementation of the HelpTopicId annotation/interface similar to the implementation of Names.named (see NamedImpl). Be aware that there are some special rules for how things like hashCode() are implemented for an annotation... NamedImpl follows those rules.

Also, using new TypeLiteral<String>(){} is wasteful... String.class could be used in its place. Furthermore, for String, int, etc. you should typically use bindConstant() instead of bind(String.class). It's simpler, requires that you provide a binding annotation, and is limited to primitives, Strings, Class literals and enums.

吃素的狼 2024-11-25 12:29:17

构造函数 Foo(String) 必须使用 @Inject 进行注释。

您应该尝试使用 Guice Named 注释,而不是使用您自己的 HelpTopicId 注释。

void configure() {
  bind(new TypeLiteral<String>(){}).annotatedWith(Names.named("A")).toInstance("1");
  bind(new TypeLiteral<String>(){}).annotatedWith(Names.named("B")).toInstance("2");
}

public class Foo {
  @Injected
  public Foo(@Named("A") String helpTopicId) {
    assertEquals("1", helpTopicId);
  }
}

如果您想推出自己的 @Named 接口实现,请查看包中的 Guice 实现 com.google.inject.name

Constructor Foo(String) has to be annotated with @Inject.

Instead of using your own HelpTopicId annotation, you should try with Guice Named annotation.

void configure() {
  bind(new TypeLiteral<String>(){}).annotatedWith(Names.named("A")).toInstance("1");
  bind(new TypeLiteral<String>(){}).annotatedWith(Names.named("B")).toInstance("2");
}

public class Foo {
  @Injected
  public Foo(@Named("A") String helpTopicId) {
    assertEquals("1", helpTopicId);
  }
}

If you want to roll out your own implementation of @Named interface, take a look at the Guice's implementation in the package com.google.inject.name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文