views:

54

answers:

1

To use gwt-dispatch we create an object like:

private static final DispatchAsync dispatchAsync = GWT.create(DefaultDispatchAsync.class);

Is there any way to do this with injection, I mean how to inject this DispatchAsync object into other classes where we need to use it.

This is something related to @Inject annotation!

+4  A: 

Yes, you can use Gin to inject your dispatch interface using the @Inject annotation.

First you need to configure a Gin binding for the DispatchAsync interface to an implementation in your Gin ClientModule.

bind(DispatchAsync.class).to(DefaultDispatchAsync.class).in(Singleton.class);

Once this is done, you can have Gin inject the dispatcher in your constructors.

class foo {
    private final DispatchAsync dispatcher;

    @Inject
    public foo(final DispatchAsync dispacher) {
        this.dispatcher = dispatcher;
    }
}
Simon.D
I found out that (with gwt-dispatch 1.1.0) you can also have the following in your Ginjector: `@GinModules({MyGinModule.class, StandardDispatchModule.class})`, where `StandardDispatchModule.class` is a gwt-dispatch class providing standard binding for `DispatchAsync`. Then you can inject a `DispatchAsync` in the constructor of classes needing it.
MarcoS