If you prefer the old method, use getInstance(Key.get(...)).  It does
exactly what you're looking for.  Just swap the annotation from the local
variable to inside the Key.get method.

FWIW, though, injecting the injector is bad practice (because you aren't
explicitly defining what your dependencies are).  MapBinder is the preferred
way to do something like this.  It's very easy to use.  Your case would be:
 GraphType enum { DOT, TEXT } // new enum class
 ...
 configure() {  // your module configuration
   MapBinder<GraphType, IDrawGraphService> mapBinder =
MapBinder.newMapBinder(binder(), GraphType.class, IDrawGraphService.class);
   mapBinder.addBinding(GraphType.DOT).to(DotGraphServiceImpl.class);
   mapBinder.addBinding(GraphType.TEXT).to(TextGraphServiceImpl.class);
 }
 ...
 @Inject Client(Map<GraphType, IDrawGraphService> services) {  // your
client classes
     IDrawGraphService dotService = services.get(GraphType.DOT); // if you
want dot
     IDrawGraphService textService = services.get(GraphType.TEXT); // if you
want text
    ...
  }

no need to even use binding annotations.

sam

On Tue, Jul 20, 2010 at 9:17 AM, Jerem's <[email protected]> wrote:

> hmm.
>
> it looks complicated.
>
> At he beginning I just wanted to do that :
>
> @ToDot
> IDrawGraphService drawDotService =
> drawGraphInjector.getInstance(IDrawGraphService.class);
>
> @ToText
> IDrawGraphService drawTextService =
> drawGraphInjector.getInstance(IDrawGraphService.class);
>
> So it was easy to choose an implementation. (just need an annotation).
> I thought that Google guice is cool.
> But guice annotation doesn't work on local variable.
>
> So if we can't do that easily. I think I prefer the old method.
>
> jérémie
>
> On 19 juil, 21:07, Sam Berlin <[email protected]> wrote:
> > Ahh -- sorry, i didn't get that you were wanting the client to choose
> which
> > one to use.  In that case, take a look at MapBinder @
> http://code.google.com/p/google-guice/wiki/Multibindings.  Set up a
> > MapBinder where you bind a Map<Class<? extends Annotation>,
> > IDrawGraphService>, and add a binding into that MapBinder for each of
> your
> > implementations.  Then your client injects  Map<Class<? extends
> Annotation>,
> > IDrawGraphService> and calls map.get(TheAnnotationItWants.class) and gets
> > the proper implementation.  (You can also use enums for the keys, or any
> > arbitrary other type, which might be clearer.)
> >
> > sam
> >
> >
> >
> > On Mon, Jul 19, 2010 at 2:48 PM, Jerem's <[email protected]> wrote:
> > > Yeah I know that. but It is not really what I want.
> > > If I do that :
> >
> > > @Inject
> > > public Client(@ToDot IDrawGraphService service)  {
> > > .. }
> >
> > > service can only be a DrawDotServiceImpl. but how can I have a
> > > DrawTextServiceImpl?
> > > I wuld like that the client can choose the implementation.
> > > Here, he can't.
> >
> > > may I can build a new class client2.
> >
> > > @Inject
> > > public Client2(@ToText IDrawGraphService service)  {
> > > .. }
> >
> > > But I don't like to have two kinds of ciient.
> >
> > > Jérémis
> >
> > > On 19 juil, 19:37, Sam Berlin <[email protected]> wrote:
> > > > Add an annotation to your injected variable, like:
> >
> > > >  @Inject public Client(@YourBindingAnnotation IDrawGraphService
> service)
> > >  {
> > > > .. }
> >
> > > > That tells Guice that when it injects the constructor with variables,
> it
> > > > should inject the graph service that was annotated with that binding
> > > > annotation.
> >
> > > > sam
> >
> > > > On Mon, Jul 19, 2010 at 12:45 PM, Jerem's <[email protected]>
> wrote:
> > > > > Thanks sam.
> >
> > > > > that's not cool that Annotation don't work for local variable.
> >
> > > > > I try the second version. IT works. thanks :).
> > > > > But I don't really like it.
> >
> > > > > So I try the first version. Bit it is not really clear in my mind.
> > > > > I create a client class :
> >
> > > > > public class Client {
> >
> > > > >        private IDrawGraphService service;
> >
> > > > >        @Inject
> > > > >        public Client(IDrawGraphService service) {
> > > > >                super();
> > > > >                this.service = service;
> > > > >        }
> >
> > > > >        public   void executeService(ISimpleGraph graph, String
> path)
> > > throws
> > > > > IOException {
> > > > >                service.draw(graph, path);
> > > > >        }
> > > > > }
> >
> > > > > but Don"t really see hos decide hos to decided is the service is a
> > > > > drawTextServiceImpl or a drawDotServiceImpl
> >
> > > > > jérémie
> >
> > > > > On 19 juil, 17:39, Sam Berlin <[email protected]> wrote:
> > > > > > Guice cannot detect annotations on local variables this way
> (although
> > > > > it's
> > > > > > an interesting idea!).  You have two options here:
> >
> > > > > > 1)  Make the variable on the class wherever this code is in and
> add
> > > an
> > > > > extra
> > > > > > module that has requestInjection(YourClass.this) in its configure
> > > method.
> > > > > >  Then Guice will inject that variable as part of its Injector
> > > creation.
> > > > > > 2) Use injector.getInstance(Key.get(IDrawGraphService.class,
> > > > > ToDot.class))
> > > > > > instead.  That tells Guice that you want to retrieve the instance
> > > defined
> > > > > by
> > > > > > the Key "@ToDot IDrawGraphService", as opposed to just
> > > > > "IDrawGraphService".
> >
> > > > > > sam
> >
> > > > > > On Mon, Jul 19, 2010 at 11:21 AM, Jerem's <[email protected]>
> > > wrote:
> > > > > > > Hello I try to use annotatoij on a local variable :
> >
> > > > > > > this is my module :
> >
> > > > > > > public class DrawModule extends AbstractModule {
> >
> > > > > > >        @Override
> > > > > > >        protected void configure() {
> >
> > >
> this.bind(IDrawGraphService.class).annotatedWith(ToDot.class).to(DrawDotSer
> > > > > viceImpl.class);
> > > > > > >        }
> > > > > > > }
> >
> > > > > > > and my Annotation look like that :
> >
> > > > > > > @Retention(RUNTIME)
> > > > > > > @Target({ LOCAL_VARIABLE, FIELD, PARAMETER })
> > > > > > > @BindingAnnotation
> > > > > > > public @interface ToDot {
> >
> > > > > > > }
> >
> > > > > > > and I try to do that :
> >
> > > > > > > Injector drawGraphInjector = Guice.createInjector(new
> > > DrawModule());
> > > > > > > @ToDot
> > > > > > > IDrawGraphService drawDotService =
> > > > > > > drawGraphInjector.getInstance(IDrawGraphService.class);
> >
> > > > > > > But I have an exception :
> >
> > > > > > > Exception in thread "main"
> > > com.google.inject.ConfigurationException:
> > > > > > > Guice configuration errors:
> >
> > > > > > > 1) No implementation for
> > > > > > > fr.irisa.cairn.simpleGraph.drawGraph.service.IDrawGraphService
> was
> > > > > > > bound.
> > > > > > >  while locating
> > > > > > > fr.irisa.cairn.simpleGraph.drawGraph.service.IDrawGraphService
> >
> > > > > > > 1 error
> > > > > > >        at
> > > > > com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:784)
> > > > > > >        at
> > > > > com.google.inject.InjectorImpl.getProvider(InjectorImpl.java:743)
> > > > > > >        at
> > > > > com.google.inject.InjectorImpl.getInstance(InjectorImpl.java:793)
> > > > > > >        at
> >
> > > fr.irisa.cairn.test.simpleGraph.runTimeTest.Launch.Main.main(Main.java:
> > > > > > > 25)
> >
> > > > > > > do you know why?
> >
> > > > > > > jérémie
> >
> > > > > > > --
> > > > > > > You received this message because you are subscribed to the
> Google
> > > > > Groups
> > > > > > > "google-guice" group.
> > > > > > > To post to this group, send email to
> [email protected]
> > > .
> > > > > > > To unsubscribe from this group, send email to
> > > > > > > [email protected]<google-guice%[email protected]>
> <google-guice%2bunsubscr...@google groups.com>
> > > <google-guice%[email protected]<google-guice%[email protected]>
> <google-guice%252bunsubscr...@g ooglegroups.com>
> >
> > > > > <google-guice%2bunsubscr...@google groups.com>
> > > > > > > .
> > > > > > > For more options, visit this group at
> > > > > > >http://groups.google.com/group/google-guice?hl=en.
> >
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > Groups
> > > > > "google-guice" group.
> > > > > To post to this group, send email to [email protected]
> .
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected]<google-guice%[email protected]>
> <google-guice%2bunsubscr...@google groups.com>
> > > <google-guice%[email protected]<google-guice%[email protected]>
> <google-guice%252bunsubscr...@g ooglegroups.com>
> >
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/google-guice?hl=en.
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "google-guice" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<google-guice%[email protected]>
> <google-guice%2bunsubscr...@google groups.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/google-guice?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to