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].
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
