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.

On 15 Jan 2014, at 3:12, Stuart McCulloch wrote:

On 15 Jan 2014, at 08:41, Jochen Wiedmann <[email protected]> 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:

        https://code.google.com/p/google-guice/wiki/ProvidesMethods

 }

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

You can find more binding examples at http://google-guice.googlecode.com/git/javadoc/com/google/inject/Binder.html and https://code.google.com/p/google-guice/wiki/Bindings

     };

Jochen




On Friday, January 10, 2014 1:46:15 PM UTC+1, Stuart McCulloch wrote:
On 10 Jan 2014, at 11:58, Fred Faber <[email protected]> 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 ) );

On Fri, Jan 10, 2014 at 4:50 AM, Jochen Wiedmann
<[email protected]>wrote:


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



--
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.
For more options, visit https://groups.google.com/groups/opt_out.


--
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.
For more options, visit https://groups.google.com/groups/opt_out.


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.


Christian Gruber :: Google, Inc. :: Java Core Libraries :: Dependency Injection
email: [email protected] :::: mobile: +1 (646) 807-9839

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to