Question

I have a custom deferred binder (rebind implementation) that instantiates objects. I would like to have some dependencies (@Inject annotated setter methods) within the instance returned by GWT.create() fulfilled by GIN. Is this possible?

So, given code such as:

Foo foo = GWT.create(Foo.class);

if foo's final implementation has:

@Inject
public void setBar(Bar bar) {
    ...
}

how do I get bar injected into the returned foo instance automatically by GIN?

Was it helpful?

Solution

Your Ginjector can have methods added to it for the purpose of injecting objects created in some other way. These must take one argument, and should specify the most specific type possible. For example, if MyViewImpl extends BaseView, and both types have dependencies to inject, but you declare

void injectBaseView(BaseView view);

in your ginjector, only the fields/setters declared on BaseView will be dealt with.

EDIT: Also, if no binding is declared, GWT.create will be used to create an instance, so you can have your cake and eat it to. One exception to that as far as I can recall, is when you want to GWT.create one type, but return another (see RPC interfaces for an example).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top