Aah! I didn't realise you could do that. I thought providers just
worked like this:
// module
bind( MyClass.class ).toProvider( MyClassProvider.class );
// logic
@Inject MyClass myClass; // Guice calls MyClassProvider#get() here
behind the scenes
I hadn't worked out that you can bind *to* a provider and then inject
the Provider interface instead of the class it provides.
Thanks, I'll give it a go that way.
Andrew.
2008/10/20 Alen Vrečko <[EMAIL PROTECTED]>:
>
> I don't get it:)
>
> //in config
> bind(DispatchHandler.class).toProvider(SoapDispatchHandlerProvider.class);
>
> //in your logic class
> public class Foo{
>
> @Inject
> Provider<DispatchHandler> myProvider;
>
> public void bar(SomeObject... oes){
> for( SomeObject o : oes)
> {
> DispatchHandler h = myProvider.get();
> // Send o to h ...
>
> }
>
> }
>
> is this not what you want?
>
> Cheers,
> Alen
>
> On 20 okt., 14:31, "Andrew Clegg" <[EMAIL PROTECTED]> wrote:
>> I needed to inject the provider so I can do things like...
>>
>> for( SomeObject o : anArray )
>> {
>> DispatchHandler h = myProvider.get();
>> // Send o to h ...
>>
>> }
>>
>> I don't think there's currently a better way to do this is there?
>>
>> Andrew.
>>
>> 2008/10/20 Alen Vrečko <[EMAIL PROTECTED]>:
>>
>>
>>
>> > I don't think you shuld be conserning yourself with binding provider
>> > directly.
>>
>> > bind(DispatchHandler.class).toProvider(SoapDispatchHandlerProvider.class);
>>
>> > now if you do
>>
>> > @Inject
>> > Provider<DispatchHandler> handler;
>>
>> > Guice will use SoapDispatchHandlerProvider. Hope it helps.
>>
>> > Cheers,
>> > Alen
>>
>> > On 20 okt., 13:04, "Andrew Clegg" <[EMAIL PROTECTED]> wrote:
>> >> Morning folks,
>>
>> >> I have a custom provider for creating SoapDispatchHandler objects,
>> >> which implement an interface called DispatchHandler.
>>
>> >> I want to set up a binding in my module such that any reference to
>> >> Provider<DispatchHandler> is injected with an instance of
>> >> SoapDispatchHandlerProvider, the provider class which builds and
>> >> returns SoapDispatchHandler objects. (Needless to say,
>> >> SoapDispatchHandlerProvider implements Provider<DispatchHandler>.)
>>
>> >> I originally did it like this:
>>
>> >> bind( new TypeLiteral<Provider<DispatchHandler>>(){} )
>> >> .to( SoapDispatchHandlerProvider.class );
>>
>> >> but this gives me an error:
>>
>> >> Binding to core guice framework type is not allowed: Provider.
>>
>> >> So to work around it I created an empty interface like so:
>>
>> >> public interface DispatchHandlerProvider extends
>> >> Provider<DispatchHandler> {}
>>
>> >> and then made SoapDispatchHandlerProvider implement
>> >> DispatchHandlerProvider instead of directly implementing
>> >> Provider<DispatchHandler>. This works; I can go:
>>
>> >> bind( DispatchHandlerProvider.class ).to(
>> >> SoapDispatchHandlerProvider.class );
>>
>> >> without any problems. But I can't help feeling empty interfaces smell
>> >> a bit funny. Is this the right way to approach this scenario?
>>
>> >> Thanks!
>>
>> >> Andrew.
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---