Correction:
public Command createCommand(Entity entity) {
Command cmd; // how to I pass in entity variable
switch (entity.getOperation()) {
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;
}
On Tuesday, June 24, 2014 12:56:12 PM UTC-7, Xin Yao wrote:
>
> 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/768b194b-adae-467e-8908-1d0c98c5a4f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.