@Provides are typically when you need to inject an instance of some library
out of your control that doesn't have any guice bindings. I try to avoid
using providers for any other use case. In your specific example, I would
do what Sam suggested and just simply inject the Settings object.

I use dropwizard a lot and typically finding myself creating hierarchical
configuration objects, and passing the sub-configuration object to a
specific module. For example,

class ApplicationConfiguration {
  public DatabaseConfiguration data;
  public TwitterConfiguration twitter;
  ...
}

And then they map one-to-one to a top-level PrivateModule which accepts the
sub-configuration object as part of its constructor. Then I can easily
either inject the configuration class privately for that sub-portion of my
application, or do whatever I need to with it (like configure an http
client in a @Provides method).

Happy Guicing!


On Fri, Mar 7, 2014 at 5:00 PM, Mikkel Petersen <[email protected]> wrote:

> Thanks for your response.
>
> It's seems overly complicated and I must admit, I don't understand it
> fully..though it properly works..I fail to see the usage of @Provides
> methods if the object provided doesn't have the object graph injected.
>
>
>
> Den fredag den 7. marts 2014 23.54.17 UTC+1 skrev Nate Bauernfeind:
>>
>> It's a bit more work, but you could consider using assisted injection for
>> this kind of use-case. My typical pattern looks like this:
>>
>> public class Example {
>>     @Inject
>>     public Example(@Assisted("host") String host
>>                    HttpClient httpClient,
>>                    ...) {
>>        ...
>>     }
>>
>>     /** This class is a Guice Assisted-Inject Factory. */
>>     public static interface Factory {
>>         Example newExample(@Assisted("host") String host);
>>     }
>> }
>>
>> ...
>>
>> public class ExampleModule {
>>   void configure() {
>>     bindFactory(Example.class, Example.Factory.class);
>>   }
>>
>>   protected <T, F> void bindFactory(Class<T> klass, Class<F>
>> factoryKlass) {
>>         bindFactory(klass, klass, factoryKlass);
>>    }
>> }
>>
>> And then you can still use a provider method (if you prefer!) and then
>> you inject the factory and the settings.
>>
>> @Provides
>> public Example someExample(Example.Factory factory, Settings settings) {
>>   return factory.newExample(settings.getHost());
>> }
>>
>> Hope that helps! I use this pattern a lot, but not often mixed with a
>> Provider -- usually I have a class that manages the multiple instances
>> key'ed by some name (like client or user).
>>
>>
>> On Fri, Mar 7, 2014 at 4:44 PM, Mikkel Petersen <[email protected]> wrote:
>>
>>> Because I want to receive other bindings:
>>> public Service someService(@Inject Settings settings)  {
>>>   SomeService s =  new SomeService(settings.getHost())
>>>   inj.injectMembers(s)
>>>   return s
>>> }
>>>
>>>
>>>
>>>
>>> Den fredag den 7. marts 2014 23.32.42 UTC+1 skrev Nate Bauernfeind:
>>>>
>>>> What about your use case prevents you from using a normal .to binding?
>>>>
>>>> bind(SomeService.class).to(SomeService.class)
>>>>
>>>> Nate
>>>>
>>>>
>>>> On Fri, Mar 7, 2014 at 4:13 PM, Mikkel Petersen <[email protected]>wrote:
>>>>
>>>>>  Hello all
>>>>>
>>>>> I have a slight problem with guice injection when using a method
>>>>> annotated with  @Provides
>>>>>
>>>>> example :
>>>>>
>>>>> @Provides
>>>>> public Service someService() {
>>>>>  return new SomeService()
>>>>> }
>>>>>
>>>>> I would like to get the current context injected in SomeService..I
>>>>> don't understand why Guice doesn't do that automatically, any particular
>>>>> reason for that ?
>>>>>
>>>>> I know I could do something like this (it works):
>>>>>
>>>>> @Provides
>>>>> public Service someService(@Inject Injector inj)  {
>>>>>   SomeService s =  new SomeService()
>>>>>   inj.injectMembers(s)
>>>>>   return s
>>>>> }
>>>>>
>>>>> But there must be a simpler way.
>>>>>
>>>>> Thanks
>>>>>
>>>>> Ps, another question, how to add syntax highlighting ?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>  --
>>>>> 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/d/optout.
>>>>>
>>>>
>>>>  --
>>> 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/d/optout.
>>>
>>
>>  --
> 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/d/optout.
>

-- 
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/d/optout.

Reply via email to