How to bind Assisted Injected class to interface?

Posted by eric2323223 on Stack Overflow See other posts from Stack Overflow or by eric2323223
Published on 2009-10-20T16:06:30Z Indexed on 2010/05/28 22:02 UTC
Read the original article Hit count: 179

Filed under:
|

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);
  }
}

© Stack Overflow or respective owner

Related posts about binding

Related posts about guice