0
votes

I have read a TON of stackoverflow and other resources trying to find an answer and maybe I just don't get it.

I am trying to inject a GIN @Singleton into several widgets and classes in my application's client code. Using Spring I can simply use the correct annotation and anything is injectable pretty much anywhere. Using GWT and GIN I can't seem to inject anywhere other than the constructors. I don't want to inject into a constructor because I am working on a very large project and it would be almost impossible.

Some of these classes are widgets and others are not. They are used throughout the application and some are injected, others are instantiated by new, and others have both, which makes changing the constructor even more difficult.

I want the code to work like this, but don't understand how it works with GIN:

@Singleton
public class ApplicationData {

   public AppInfo getAppInfo() {
      return new AppInfo("My App Info");
   }
}

public class MyClass{
   @Inject protected ApplicationData applicationData;

   public void someMethod() {
      doSomething(applicationData.getAppInfo());
   }
}

Someone else coded this to work in a Presenter and I kind of understand why it works, but I don't totally grasp the concepts. They are no longer around to ask.

Here is a hack that I have so that it will work, but I just don't like it:

@GinModules(GinModule.class)
public interface Injector extends Ginjector {
   final Injector INSTANCE = GWT.create(Injector.class);

   ApplicationData getApplicationData();
}

public class MyWidget extends Composite implements TakesValue<Integer>{

   public static interface MyWidgetUiBinder extends UiBinder<FormPanel, MyWidget>}

   private MyWidgetUiBinder uiBinder = GWT.create(MyWidgetUiBinder.class);
   private ApplicationData appData;

   public ArrivalTimeWidget() {
      appData = Injector.INSTANCE.getApplicationData();
      initWidget(uiBinder.createAndBindUi(this));
      someMethod(appData.getAppInfo());
   }
}

Please help me to do this correctly instead of using a hack. And please treat me like I am stupid, with full explanations, because when it comes to this, I am stupid.

1
I'm curious how Spring would make it different: if GIN doesn't inject your field, it must be because it's not in charge of instantiating the class; I'd assume the same would be true with Spring DI.Thomas Broyer
Please go through the link. It might help you.stackoverflow.com/questions/6495303/…PVR
@Ranna, so this is the only thing useful that I see in that link: "To have dependency injection you need to initialize your classes through GIN, using injector (as you noted above)." So, it seems that all 1000 of my classes would need to be instantiated by GIN in order to use dependency injection? That seems ridiculous, if not stupid. Isn't there a better way, or has GIN just not evolved enough? My way shown above works, so why can't GIN use the same philosophy to inject field members with annotations? Seems very limited in nature. Either that, or I still just don't get it.user1209809
@Thomas Spring can scan classes for dependencies that are annotated, there isn't a rule that requires a user to instantiate everything through a single entry point like GIN (at least my understanding so far).user1209809
Yes, but it won't inject objects that it doesn't create: how would it know those objects have been created and need to be injected? AFAICT Spring does not instrument your code with an agent… Configuration is done with classpath scan and auto-wiring, but once configured it works almost the same.Thomas Broyer

1 Answers

0
votes

The answer is that I can only Inject from objects that have been Injected themselves, as @Thomas Broyer said. I was creating "new" objects and not injecting them.