Binding a constructor argument based on the Annotation of the class

被刻印的时光 ゝ 提交于 2020-01-17 05:14:20

问题


I have an interface: InterfaceA.

I have a class: ConcreteA.

I also have two annotations: @AnnotA and @AnnotB.

I have done the following bindings:

bind(InterfaceA).annotatedWith(AnnotA).to(ConcreteA);
bind(InterfaceA).annotatedWith(AnnotB).to(ConcreteA);

Next, class ConcreteA has a constructor that takes a String argument called hostName.

class ConcreteA
{
    @Inject
    public ConcreteA(@Named("hostName") hostName) {
    }

    ... <rest of class>
}

I need code to describe the following:

If ConcretaA is using @AnnotA then bind hostName with String value of 'localhost'

If ConcreteA is using @AnnotB then bind hostName with String value of 'externalhost'

Any ideas of a solution for this?


回答1:


I think in your case, you might consider putting each binding in its own private module.

class MyModule() { 
  install(new PrivateModule() {
    public void configure() {
       bind(InterfaceA).to(ConcreteA);
       bind(String.class).annotatedWith(Names.named("hostName").to("localhost");
       expose(InterfaceA).annotatedWith(AnnotA.class);
    }});
  install(new PrivateModule() {
    public void configure() {
       bind(InterfaceA).to(ConcreteB);
       bind(String.class).annotatedWith(Names.named("hostName").to("externalhost");
       expose(InterfaceA).annotatedWith(AnnotB.class);
    }});
}

(This is from memory and syntax may not be 100% correct.)

For more detail, start with the Guice FAQ, and search that page for "robot legs" -- I'm not joking :)

There is even more detail behind the two additional links from that section of the FAQ.




回答2:


Here is a complete example code listing for solving the robot two legs problem:

http://pastie.org/368348



来源:https://stackoverflow.com/questions/7660374/binding-a-constructor-argument-based-on-the-annotation-of-the-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!