gwtdependency-injectionguicegwt-gin

Can I inject objects in regular classes in GWT using GIN?


For instance, I have the GIN module which includes the binding for class A. While in class B (B is not bind using GIN), can I just simply use:

@Inject private A a;

to inject class A? I tried in my project, and looks like I got null pointer for object a. No idea why.


Solution

  • Because you need to instantiate your B class with GIN also.

    For example with @UiFields you can use (provided true) and then inject them in the constructor, like this:

        /*Class B is not binded by GIN*/
        public class B {
            @Inject
            C cInstance; //C is binded by GIN
        }
    
    /*Class A is binded with GIN*/
        public class A extends ViewImpl{
    
                    @UiField(provided=true)
                    B myWidget;
    
                    //and then
    
                    @Inject
                    public A (UiBinder binder, B myWidget){
                       this.myWidget = myWidget;  //cInstance inside of it is injected!
                    }
            }
    

    After this kind of injection of B, all the @Inject annotations inside of B should resolve as expected.

    And in case you instantiate A with GWT.create / new keyword - myWidget reference for B instance will be null also