Binding to a provider instance

1,379 views
Skip to first unread message

Jochen Wiedmann

unread,
Jan 10, 2014, 4:50:19 AM1/10/14
to google...@googlegroups.com

Hi,

I am currently converting an existing application to Guice. To simplify the migartion, I'd like to do somehing like


    Foo someInstance;
    Provider<Foo> myProvider = new Provider<Foo>(){
        Foo get() { return someInstance; }
    }

    binder.bind(IFoo.class).toProviderInstance(myProvider);

Is that possible?

Thanks,

Jochen

       

Stephan Classen

unread,
Jan 10, 2014, 5:03:19 AM1/10/14
to google...@googlegroups.com
There is not bind(XXX).toProviderInstance(YYY)
Instead you can directly use the following:

Foo someInstance = new Foo();
binder.bind(IFoo.class).toInstance(someInstance);
> --
> 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 google-guice...@googlegroups.com.
> To post to this group, send email to google...@googlegroups.com.
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.

Jochen Wiedmann

unread,
Jan 10, 2014, 6:25:45 AM1/10/14
to google...@googlegroups.com


On Friday, January 10, 2014 11:03:19 AM UTC+1, scl wrote:
There is not bind(XXX).toProviderInstance(YYY)
Instead you can directly use the following:

Foo someInstance = new Foo();
binder.bind(IFoo.class).toInstance(someInstance);

Thanks, very much.

Unfortunately, that's not what I want. The application was previously made to work with provider instances. (so-called factories)
To minimize changes, I'd like to leave it at that.

Jochen

Fred Faber

unread,
Jan 10, 2014, 6:58:25 AM1/10/14
to google...@googlegroups.com
binder.bind(IFoo.class).toProvider(myProvider) does this.


--

Stephan Classen

unread,
Jan 10, 2014, 7:07:11 AM1/10/14
to google...@googlegroups.com
It doesn't matter how you make you bindings.
You can always inject a Provider<Foo>.
Guice will create an Provider instance for you even if you bind to a class or an instance.

Stuart McCulloch

unread,
Jan 10, 2014, 7:46:15 AM1/10/14
to google...@googlegroups.com
On 10 Jan 2014, at 11:58, Fred Faber <ffa...@faiser.com> wrote:

> binder.bind(IFoo.class).toProvider<https://google-guice.googlecode.com/git/javadoc/com/google/inject/binder/LinkedBindingBuilder.html#toProvider(com.google.inject.Provider<?
> extends T>)>(myProvider) does this.

and if you have existing javax.inject.Provider instances you can use Providers.guicify(myStandardProvider) to convert them to com.google.inject.Provider

http://google-guice.googlecode.com/git/javadoc/com/google/inject/util/Providers.html

bind( IFoo.class ).toProvider( Providers.guicify( myStandardProvider ) );

Jochen Wiedmann

unread,
Jan 15, 2014, 3:41:04 AM1/15/14
to google...@googlegroups.com

Thanks very much, but could someone help me by fixing the following code (I hope the intention is clear):

    private void bindList(Binder pBinder) {
        final List<?> list = new ArrayList<Object>();
        final Provider<O extends List> provider = new Provider<O>(){
            public O get() {
                return list;
            }
        };
        pBinder.bind(List.class).toProvider((com.google.inject.Provider<? extends List>) provider);
    }

Additionally, is it possible to have something like

        private <O> void bind(Binder pBinder, Class<O> pInterfaceClass, O pImplementation) {
             // ?
        };

Jochen

Stuart McCulloch

unread,
Jan 15, 2014, 6:12:06 AM1/15/14
to google...@googlegroups.com
On 15 Jan 2014, at 08:41, Jochen Wiedmann <jochen....@gmail.com> wrote:


Thanks very much, but could someone help me by fixing the following code (I hope the intention is clear):

    private void bindList(Binder pBinder) {
        final List<?> list = new ArrayList<Object>();
        final Provider<O extends List> provider = new Provider<O>(){
            public O get() {
                return list;
            }
        };
        pBinder.bind(List.class).toProvider((com.google.inject.Provider<? extends List>) provider);

Does your provider implement com.google.inject.Provider?  If it doesn’t - for example, say it just implements javax.inject.Provider - then you should use:

pBinder.bind(List.class).toProvider(Providers.guicify(provider));

BTW, if you don’t want to write lots of Provider classes then you can also use @Provides methods in your module:


    }

Additionally, is it possible to have something like

        private <O> void bind(Binder pBinder, Class<O> pInterfaceClass, O pImplementation) {
             // ?

Sure:

pBinder.bind(pInterfaceClass).toInstance(pImplementation);

Note that this will automatically do field and setter injection of any members annotated with @Inject in the instance - if you don’t want this then use:

pBinder.bind(pInterfaceClass).toProvider(Providers.of(existingInstance));

Christian Gruber

unread,
Jan 15, 2014, 11:22:34 AM1/15/14
to google...@googlegroups.com
Side note - I believe we have a change in the works to make Guice (and
specifically LinkedBindingBuilder) handle javax.inject.Provider bindings
natively. It got disrupted by holidays and other priorities, but in the
long run I don't think we'll need Providers.guicify() in client module
code.

But for now, Providers.guicify() is needed.

c.
Christian Gruber :: Google, Inc. :: Java Core Libraries :: Dependency
Injection
email: cgr...@google.com :::: mobile: +1 (646) 807-9839
Reply all
Reply to author
Forward
0 new messages