7
我也是Java初學者和IoC。如何做東西:具有多重綁定的IoC問題
public class Foo{
//private Bar bar; //Bar is an interface private int var; public Foo(){ } public void setVar(int var){
this.var = var;
} public Bar getBar(){
if(var==1){
return new BarImpl1(); //an implemantation of Bar interface
}
else if(var==2){
return new BarImpl2(); //an implemantation of Bar interface
}
else{
return new BarImpl(); //an implemantation of Bar interface
}
}
}
in IoC way on Guice example?
public class Foo{
private Bar bar; //Bar is an interface private int var; @Inject public Foo(Bar bar){
this.bar = bar;
} public void setVar(int var){
this.var = var;
} public Bar getBar(){
return bar; // or what else??
}
}
我應該如何配置我的注射器?
@Override
protected void configure() {
bind(Bar.class).to(BarImpl.class);
//and what else??
}
非常感謝你。這正是你如何猜測的。感謝你,我終於明白了Provider和MapBinder :) – kospiotr 2010-04-21 17:25:49