2013-12-23 28 views
1

我不明白我怎么可以在Guice中使用注入到特定的实例。我试过这个:Java Guice - 为特定实例使用注入?

@Inject(optional=true) 
    public void setCarpenter(@Named("setCarpenter") Carpenter carpenter){ 
     System.out.println("Assigned carpenter for Table"); 
     this.carpenter = carpenter; 
    } 


bind(Table.class).annotatedWith(Names.named("table1")).toInstance(new Table(4, new Metal())); 
bind(Table.class).annotatedWith(Names.named("table2")).toInstance(table); 
bind(Carpenter.class).annotatedWith(Names.named("setCarpenter")).toInstance(new Carpenter()); 

而且它使用setCarpenter这两个表实例。我如何指定设置为特定实例?

回答

1

我想你可能在寻找解决“机器人腿”问题的方法。

查看wiki的详细信息here

+0

我知道这一个。也许有一个更简单/更漂亮的解决方案呢? – Andrius