Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top