Hi,
I have a class Command that I want to leverage Guice's injector as follows. 
Class Backend is a singleton and should be injected by Guice. But I'd like 
to be able to pass in argument entity at runtime.

public class Command {
  private final Backend backend;

  @Inject
  public Command(Backend backend, @Assisted Entity entity) {
      this.backend = backend.
  }
}

I wonder if I can do the following. I looks with injector.getInstance does 
not let me specify an argument, i.e. entity, to pass in.

@Singleton
public class CommandFactory {

  private Injector injector;

  public StepCommandFactory() {
    injector = Guice.createInjector(new ApiFeModule()); // ApiFeModule is a 
module to be used
  }

  public Command createCommand(Entity entity) {
    switch (entity.getOperation()) {
      Command cmd;
      case CREATE_VM:
        cmd = injector.getInstance(VMCreateStepCmd.class);
        break;
      case CREATE_DISK:
        cmd = injector.getInstance(DiskCreateStepCmd.class);
        break;
      case ATTACH_DISK:
        cmd = injector.getInstance(DiskAttachStepCmd.class);
        break;
      default:
        throw new InternalException(String.format("Invalid Operation %s to 
create command",
            entity.getOperation()));
    }

    return cmd;
  }
}

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/4fd8090c-a0fd-4546-aa05-bbfe164c4d57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to