Below is my code. When I running this program, I will get an error
"Could not find a suitable constructor in java.lang.Integer. Classes
must have either one (and only one) constructor annotated with @Inject
or a zero-argument constructor that is not private.". How can I pass a
parameter to the provider?
thanks.
public class ProviderDemo {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyModule1());
Interface1 in = injector.getInstance(Interface1.class);
System.out.println(in);
}
}
class MyModule1 extends AbstractModule{
@Override
protected void configure() {
}
@Provides
public Interface1 provideInterface1(int type){
if(type == 1){
return new Imple1();
}else if(type ==2){
return new Imple2();
}
return null;
}
}
interface Interface1{
public void sayHello();
}
class Imple1 implements Interface1{
@Override
public void sayHello() {
System.out.println("imple 1");
}
}
class Imple2 implements Interface1{
@Override
public void sayHello() {
System.out.println("imple 2");
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---