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.

Reply via email to