"I'd like that Guice call me when it tries to inject, and so I will return
the "better" instance. "

Isn“t this the provider role?

You can extend the Provider class too so you can inject other "context"
objects into it and decide which instance would be best for the moment.

=)

Hope this can help you.

Best regards,

Marcos Alcantara

On Tue, Jan 19, 2010 at 11:56 AM, Anthony MULLER
<[email protected]>wrote:

> Thanks Fred! But my bindings are "modular"... I don't know them before
> startup...
>
> In the example, imagine the injectee must be the instance with the time the
> closest of injection time. Or, if no "@Timed" is provided, the default
> implementation must be injected.
>
> // Example
> public class MaClasse {
>        @Inject // Default injection
>        private ISomething something;
>
>        @Inject @Timed // Timed injection
>        private ISomething something;
> }
>
> Bindings :
> ISomething --> DefaultSomethingImpl // Default
>
> "Contributors" can provided bindings like:
> ISomething @Named("8:15") --> SomethingMorningImpl // Optional
> ISomething @Named("12:30") --> SomethingMiddayImpl // Optional
> ISomething @Named("20:55") --> SomethingEveningImpl // Optional
>
> As you see, I don't know the list in advance...
>
> The provider could be a solution, but the problem is I don't have
> information on the injectee (in my case: the attached annotations)... So, I
> can not control the injection as I wish :-/
>
> Maybe it is possible to do what I wish by using Guice 2.0 and its SPI?
>
> Regards,
> Anthony
>
>
> 2010/1/19 Fred Faber <[email protected]>
>
>>  Anton's suggestion should do the trick:
>>
>> class ISomethingProvider implements Provider<ISomething> {
>>
>>  @Inject @ReportsTimeOfDay
>>  TimeOfDayReporter timeOfDayReporter;
>>
>>  @Inject @Name("morning")
>>   Provider<ISomething> somethingMorningProvider;
>>
>>  @Inject @Name("midday")
>>   Provider<ISomething> somethingMiddayProvider;
>>
>>  @Inject @Name("evening")
>>   Provider<ISomething> somethingEveningProvider;
>>
>>  @Override
>>  public ISomething get() {
>>      TimeOfDay timeOfDay = timeOfDayReporter.getTimeOfDay();
>>      if (timeOfDay.isMorning()) {
>>        return somethingMorningProvider.get();
>>     } else if (....) {
>>       ....
>>     }
>>  }
>> }
>>
>>
>> and then:
>>
>> bind(ISomething.class)
>>   .toProvider(ISomethingProvider.class);
>>
>>
>> * * *
>>
>> "injection context" is a vague and simultaneous powerful concept, which is
>> different from application to application, and even injection to injection.
>>  the provider construct allows flexible definitions of this context to suit
>> each setting.
>>
>> -Fred
>>
>> On Tue, Jan 19, 2010 at 9:18 AM, Anthony MULLER <[email protected]
>> > wrote:
>>
>>> Ok!
>>>
>>> Imagine I have several bindings for ISomething :
>>>
>>> ISomething @Named("morning") --> SomethingMorningImpl
>>> ISomething @Named("midday") --> SomethingMiddayImpl
>>> ISomething @Named("evening") --> SomethingEveningImpl
>>>
>>> At runtime, I know what time is it and I wish that the proper instance is
>>> injected according the time:
>>>
>>> public class MaClasse {
>>>        @Inject
>>>        @Timed
>>>        private ISomething something;
>>> }
>>>
>>> I'd like that Guice call me when it tries to inject, and so I will return
>>> the "better" instance.
>>>
>>> As you see, the framework is not able to decide alone in that case. But
>>> if it is possible to plug behaviour at injection time, this case can be
>>> resolved.
>>>
>>> Hoper I'm clearer :-p
>>>
>>> Regards,
>>> Anthony
>>>
>>>
>>> 2010/1/19 Fred <[email protected]>
>>>
>>>> Could you please give an example?  I'm having a hard time interpreting
>>>>
>>>> what you mean.
>>>>
>>>> On Jan 19, 8:15 am, Anthony MULLER <[email protected]> wrote:
>>>> > Hello,
>>>> >
>>>> > I can't remember... I work with Guice 1.0.
>>>> >
>>>> > Is is possible to take the hand when it is written:
>>>> >
>>>> > public class MaClasse {
>>>> >
>>>> >        @Inject
>>>> >        private ISomething something;
>>>> >
>>>> > }
>>>> >
>>>> > I'd like to write a specific resolution, to choose the "best"
>>>> binding....
>>>> >
>>>> > Thanks,
>>>> > Anthony
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "google-guice" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<google-guice%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/google-guice?hl=en.
>>>>
>>>>
>>>>
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "google-guice" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<google-guice%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/google-guice?hl=en.
>>>
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "google-guice" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<google-guice%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-guice?hl=en.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply via email to