Thanks for your reply, Jean-Francois,

But really, I need to bind SimpleCommand to Executable. I actually
have two Exectuable implementations, SimpleCommand and
PipelinedCommand, both of them is instantiated by a String, which
comes from user input. There is a command engine responsible to handle
the Executable passed in.
Do you have any idea?


On Oct 21, 7:16 am, Jean-Francois Poilpret <jfpoilp...@yahoo.fr>
wrote:
> eric wrote:
> > I have a SimpleCommand that has a String type Assisted injected:
>
> > 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;
> >   }
> > }
>
> > And I bind SimpleCommand to Executable as below:
>
> > 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 SimpleCommnd instance using:
> > Guice.createInjector(new MyModule()).getInstance
> > (CommandFactory.class).create("sample command");
>
> > I got such 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)
>
> > Could any one help me?
>
> When you use asisisted injection, that means the instances created by
> your factories are not candidates for being injected elsewhere.
> The reason for this is simple: they cannot be instantitated by Guice
> directly but only by your program; in addition, with CommandFactory you
> can create many instances of SimpleCommand, how would Guice know which
> one to inject?
>
> Thus the reason for this error is your binding of Exceutable to
> SimpleCommand. Simply remove it. Instead inject CommandFactory when you
> need a SimpleCommand and instantiate it yourself.
> This binding is telling Guice: "whenever you need to inject an
> Executable, instantiate SimpleCommand and inject the instance instead."
> This can work only if Guice can instantiate SimpleCommand all by itself
> (which means without assisted injection).
>
> Why did you feel the need for creating this binding?
>
> Cheers
>
> Jean-Francois
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to google-guice@googlegroups.com
To unsubscribe from this group, send email to 
google-guice+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to