On Tue, Nov 29, 2011 at 11:39 AM, Xanadu <[email protected]> wrote:

> Thanks for your answer !
>
> @Noc
> You're right I think that factory is the way to go but I thought it
> was only use when  input parameters are needed.
>
> @Sam
> 1) if I use the toProvider() method, my object is not construct lazily
> so that's not ok with my requirements.
> 2) Yes the compilation is ok but there is always the same runtime
> error :  "Binding to Provider is not allowed"
>

It looks like you may be confusing a Provider with what is provided.  Given
some code like this:

  MyProvider implements Provider<Foo> { Foo get() { return new FooImpl(); }

then you would bind this using:
   bind(Foo.class).toProvider(MyProvider.class);

This tells Guice that when you want to inject Foos, it will get a Foo by
calling MyProvider.get().  If you inject a Provider<Foo>, calling
provider.get() will end up calling MyProvider.get() also.

But, you can also just do something like:
    bind(Foo.class).to(FooImpl.class);

and even with that, you can still inject a Provider<Foo>.  Guice will only
construct a FooImpl when you call provider.get().

The error message coming from MapBinder is a little confusing, but it's
actually stemming because of the way you're defining the MapBinder itself.
 You're saying:
  MapBinder.newMapBinder(binder(), new TypeLiteral<String>() {},
new TypeLiteral<Provider<MyInterface>>(){});

but what you actually want to say is:
  MapBinder.newMapBinder(binder(), String.class, MyInterface.class);

This will make it clearer how you're supposed to bind the values,
and MapBinder will then let you inject a Map<String,
Provider<MyInterface>>, which will accomplish what you want.

sam


>
> Here are my classes :
>
> public interface MyInterface {
>    public void doAction();
> }
>
> public interface MyInterfaceImpl1 {
>    public void doAction(){
>        ...
>    }
> }
>
> public interface MyInterfaceImpl2 {
>    public void doAction(){
>        ...
>    }
> }
>
> public class MyInterfaceImpl1Provider implements Provider<MyInterface>
> {
>        @Override
>        public MyInterface get() {
>                return new MyInterfaceImpl1();
>        }
> }
>
> public class MyInterfaceImpl2Provider implements Provider<MyInterface>
> {
>        @Override
>        public MyInterface get() {
>                return new MyInterfaceImpl2();
>        }
> }
>
> public class MyModule extends AbstractModule {
>        @Override
>        protected void configure() {
>            MapBinder<String, Provider<MyInterface>> providerMap =
> MapBinder.newMapBinder(binder(), new TypeLiteral<String>() {}, new
> TypeLiteral<Provider<MyInterface>>(){});
>
> providerMap.addBinding("myKey1").to(MyInterfaceImpl1Provider.class);
>
> providerMap.addBinding("myKey2").to(MyInterfaceImpl2Provider.class);
>        }
> }
>
> Maybe my configuration is wrong...
>
>
> On 29 nov, 14:29, Sam Berlin <[email protected]> wrote:
> > In Guice, if you want to bind to a Provider, you should use the
> > *toProvider*(..)
> > methods.  However, keep in mind that Guice will allow you to inject a
> > Provider<T> of something even if it was bound using to(..).  MapBinder
> > specifically allows you to inject a Map<K, Provider<T>>, even if all the
> > mapbindings were added using mapBinder.addBinding(K).to(T.class).
> >
> > To Guice, injecting Provider<T> is effectively the same as injecting T
> > directly, except that Guice won't attempt to construct the T until you
> call
> > provider.get() (which is your requirement: making construction happen
> > lazily).
> >
> > sam
> >
> >
> >
> >
> >
> >
> >
> > On Tue, Nov 29, 2011 at 8:18 AM, Xanadu <[email protected]> wrote:
> > > hey !
> >
> > > I have several implementations for one interface. So I'm using
> > > mapbinder to get one implementation for one string key. I need those
> > > implementations to be constructed lazily : that's why I decided to use
> > > provider for each implementation.
> >
> > > My problem is that I have the following error :
> > >  "Binding to Provider is not allowed"
> >
> > > Is this normal and do you know any workaround ?
> >
> > > Thanks.
> >
> > > --
> > > 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.
>
> --
> 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.
>
>

-- 
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