Voici le problème que je rencontre:Comment lier la classe Assisted Injecté à l'interface?
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);
}
}
Lorsque je tente d'obtenir par exemple de SimpleCommand en utilisant:
Guice.createInjector(new MyModule()).getInstance(CommandFactory.class).create("sample command");
J'ai eu cette erreur:
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)
Donc mon problème est de savoir comment puis-je lier SimpleCommand à Executable lorsque SimpleCommand a le paramètre Assisted Injected?
Voici le CommandFactory et sa mise en œuvre:
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);
}
}
Qu'est-ce commandement? L'usine ne devrait-elle pas retourner Executable ou SimpleCommand? – ColinD
Aussi, pourquoi avez-vous une implémentation de CommandFactory? L'idée est que Injection assistée crée l'implémentation pour vous. – ColinD