JavaGuice-对特定实例使用注入?
我不明白如何在特定实例的Guice中使用注入。我试过这个:JavaGuice-对特定实例使用注入?,java,dependency-injection,guice,Java,Dependency Injection,Guice,我不明白如何在特定实例的Guice中使用注入。我试过这个: @Inject(optional=true) public void setCarpenter(@Named("setCarpenter") Carpenter carpenter){ System.out.println("Assigned carpenter for Table"); this.carpenter = carpenter; } bind(Table.class).a
@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
。如何指定设置为特定实例?我认为您可能正在寻找“机器人腿”问题的解决方案
有关详细信息,请参见wiki我知道这一点。也许有更简单/更漂亮的解决方案?