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"

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.

Reply via email to