Sure, this is a default feature of Guice:

    public class RiakModule extends AbstractModule {

        @Override
        protected void configure() {
            PBClientConfig conf = new PBClientConfig.Builder()
                    .withHost("127.0.0.1")
                    .withPort(8097)
                    .build();

            bind(PBClientConfig.class).toInstance(conf);
        }

        @Provides
        public IRiakClient getClient(PBClientConfig config) {
            return RiakFactory.newClient(config)
        }
    }

In this case no binding is required for IRiakClient, as it's automatic by
the @Provides method.
You can also bind the config with a binding annotation (in this case the
getClient() method's PBClientConfig parameter must be annotated by the same
annotation. You can also annotate the @Provides method with a binding
annotation and/or make it singleton by using the @Singleton annotation.

HTH



--
L


On Fri, Nov 1, 2013 at 3:59 PM, Ari King <[email protected]>wrote:

>
>
> On Thursday, October 31, 2013 7:20:25 PM UTC-4, Laszlo Ferenczi wrote:
>>
>> Hi,
>>
>> As simple as:
>>
>> bind(IRiakClient.class).**toInstance(client);
>>
>> Create the client instance in the same module where the binding is.
>> If you'd like to use multiple instances just bind them with an annotation:
>>
>> bind(IRiakClient.class).**annotatedWith(MyAnnotation.**
>> class).toInstance(client);
>>
>> @Inject
>>  @MyAnnotation
>> IRiakClient client;
>>
>> HTH
>>
>>
>> --
>> L
>>
>>
> Hi,
>
> Thank you for the example; it was very helpful. However, I do have a
> follow-up question. I understand that I can create a method annotated with
> @Provides that can create the desired object instance. In the above
> scenario, the client values are hardcoded, in my situation the underlying
> framework at bootup creates a configuration object to use with client
> construction. Can this configuration object be passed to the @Provides
> method within the AbstractModule implementation? If yes, how?
>
>
> -Ari
>
> --
> 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.

Reply via email to