我不明白如何在 Guice 中对特定实例使用注入(inject)。我试过这个:
@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 here
关于Java Guice - 对特定实例使用注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20749404/