How to bind Assisted Injected class to interface?
- by eric2323223
Here is the problem I met:
Class SimpleCommand implements Executable{
private final ConfigManager config;
private String name;
@Inject
public SimpleCommand(ConfigManager config, @Assisted String name){
this.config = config;
this.name = name;
}
}
Class MyModule extends AbstractModule{
@Override
protected void configure() {
bind(CommandFactory.class).toProvider(FactoryProvider.newFactory(CommandFactory.class, SimpleCommand.class));
bind(Executable.class).to(SimpleCommand.class);
}
}
When I try to get instance of SimpleCommand using:
Guice.createInjector(new MyModule()).getInstance(CommandFactory.class).create("sample command");
I got this error:
1) No implementation for java.lang.String annotated with @com.google.inject.assistedinject.Assisted(value=) was bound.
while locating java.lang.String annotated with @com.google.inject.assistedinject.Assisted(value=)
for parameter 2 at model.Command.<init>(SimpleCommand.java:58)
at module.MyModule.configure(MyModule.java:34)
So my problem is how can I bind SimpleCommand to Executable when SimpleCommand has Assisted Injected parameter?
Here is the CommandFactory and its implementation:
public interface CommandFactory{
public Command create(String name);
}
public class GuiceCommandFactory implements CommandFactory{
private Provider<ConfigManager> configManager ;
@Inject
public GuiceCommandFactory(Provider<ConfigManager> configManager){
this.configManager = configManager;
}
public Command create(String cmd){
return new Command(configManager.get(), cmd);
}
}